Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with NSSet (remove object and iterate)

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

like image 288
eemceebee Avatar asked Nov 25 '25 21:11

eemceebee


1 Answers

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];
like image 121
Mike Weller Avatar answered Nov 28 '25 17:11

Mike Weller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!