If I have an NSSet
of NSString
objects, how can I join them together to create a single NSString
?
An NSSet can be created through the setWithObjects: class method, which accepts a nil -terminated list of objects. Most of the examples in this module utilize strings, but an NSSet instance can record any kind of Objective-C object, and it does not have to be homogeneous.
The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.
NSSet declares the programmatic interface for static sets of distinct objects. You establish a static set's entries when it's created, and can't modify the entries after that. NSMutableSet , on the other hand, declares a programmatic interface for dynamic sets of distinct objects.
NSSet
's -allObjects
method will return an NSArray
of objects in the receiver.
So, knowing this, it's pretty simple:
[[set 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