Im trying to print out an NSSet on a single line, comma separation but no trailing comma or space. how can i do this?
i know that this works for an array:
NSMutableString *outputStringArray = [[NSMutableString alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:10];
for (int k = 0; k < [myArray count]; k++) {
[outputStringArray appendFormat:@"%@, ", [myArray objectAtIndex:k]];
}
NSLog(@"%@", [outputStringArray substringToIndex:[outputStringArray length] - 2]);
but since sets don't have indexing i cant do this.
thanks
You can always make an array out of a set, like this:
NSArray *myArray = [mySet allObjects];
After that, you can get your string with componentsJoinedByString:
:
NSString *str = [myArray componentsJoinedByString:@", "];
Of course you can achieve the same effect with a simple loop similar to the one in your post:
BOOL isFirst = YES;
for (id element in mySet) {
if (!isFirst) {
[outputStringArray appendString:@", "];
} else {
isFirst = NO;
}
[outputStringArray appendFormat:@"%@", element];
}
Get the objects in your set as an array and use componentsJoinedByString:
NSSet *myset = ....;
NSString *joinedString = [[myset allObjects] componentsJoinedByString:@", "];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With