Question

I am currently working on a project that uses CoreData and relations are using NSSet. I have currently 2 problems :

  1. How to iterate over an NSSet using an index ? --> SOLVED

  2. How to remove a specific object ? => I guess I need to iterate and check for the object ?

hmm looks like I also have a problem adding an object ? Whats wrong with this :

    [mySet setByAddingObject:info];

Thanks for the help.

mcb

Was it helpful?

Solution

A set is an unordered container which means you cannot iterate it using an index. You can use [set allObjects] to get an array, but bear in the mind the ordering is not going to be consistent each time you execute the code. So you might want to sort that array before iterating it, depending on what you are doing.

To remove an object you have to have an instance of NSMutableSet and use the removeObject: message. If you only have an NSSet and wish to create a new set with a certain item removed, you would use code like this:

NSSet *mySet = /* ... */;
NSMutableSet *mutable = [NSMutableSet setWithSet:mySet];
[mutable removeObject:myObject];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top