0

我遍历这样的数组。

NSArray *array = [[currentRaum raumattribute] allObjects];

    NSString *compositeString = [ [ NSString alloc ] init ];
    for( Raumattribute *attr in array ){
        compositeString = [ NSString stringWithFormat:@"%@ %@",compositeString, attr.attributname ];
    [raumAttributLabel setText:compositeString];

我想要这样的输出----> Cat,Dog,Mouse

但如果我这样做:

compositeString = [ NSString stringWithFormat:@"%@ %@,",compositeString, attr.attributname ];

输出是猫,狗,老鼠,

或者如果我这样做:

compositeString = [ NSString stringWithFormat:@"%@ ,%@",compositeString, attr.attributname ];

输出它的 ,Cat,Dog,Mouse

有解决办法吗?

4

1 回答 1

4

您可以使用数组valueForKey:componentsJoinedByString:方法来做到这一点:

NSString * compositeString =  [[array valueForKey:@"attributname"] componentsJoinedByString:@","]

valueForKey:将为每个数组元素创建属性名称值数组

componentsJoinedByString:将这些值连接成一个用逗号分隔的字符串

于 2012-04-23T14:36:32.120 回答