gpt4 book ai didi

ios - NSSet 以逗号分隔的字符串

转载 作者:可可西里 更新时间:2023-11-01 05:01:43 25 4
gpt4 key购买 nike

我有下一个代码,用于将 NSSet 转换为以逗号分隔的字符串:

-(NSString *)toStringSeparatingByComma
{
NSMutableString *resultString = [NSMutableString new];
NSEnumerator *enumerator = [self objectEnumerator];
NSString* value;
while ((value = [enumerator nextObject])) {
[resultString appendFormat:[NSString stringWithFormat:@" %@ ,",value]];//1
}

NSRange lastComma = [resultString rangeOfString:@"," options:NSBackwardsSearch];
if(lastComma.location != NSNotFound) {
resultString = [resultString stringByReplacingCharactersInRange:lastComma //2
withString: @""];
}
return resultString;
}

它似乎有效,但我在这里收到两个警告:

1. format string is not a string literal (potentially insecure)

2. incompatible pointer types assigning to nsmutablestring from nsstring

如何重写它以避免警告?

最佳答案

还有另一种方法可以用更少的代码行来实现您想要做的事情:

您可以使用以下方法获取 NSSet 对象数组:

NSArray *myArray = [mySet allObjects];

您可以将数组转换为字符串:

NSString *myStr = [myArray componentsJoinedByString:@","];

关于ios - NSSet 以逗号分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238152/

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