gpt4 book ai didi

ios - 与 ","分开?

转载 作者:行者123 更新时间:2023-11-28 19:19:14 27 4
gpt4 key购买 nike

我像那样遍历数组。

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

有解决办法吗?

最佳答案

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

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

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

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

关于ios - 与 ","分开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10282597/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com