NSSet for separating comma strings

I have the following code to convert an NSSet to a comma separated line:

-(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; } 

It seems to work, but I get two warnings here:

 1. format string is not a string literal (potentially insecure) 2. incompatible pointer types assigning to nsmutablestring from nsstring 

How to rewrite it to avoid warnings?

+4
source share
2 answers

There is another way to achieve what you are trying to do with fewer lines of code:

You can get an array of NSSet objects using:

 NSArray *myArray = [mySet allObjects]; 

You can convert the array to a string:

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

stringByReplacingCharactersInRange Returns the type of the NSString method. You assign it to NSMutableString . Use a mutable copy.

 resultString = [[resultString stringByReplacingCharactersInRange:lastComma //2 withString: @""]mutablecopy] 
+1
source

Source: https://habr.com/ru/post/1497086/


All Articles