开发者

Problems with NSSet (remove object and iterate)

开发者 https://www.devze.com 2023-01-02 01:06 出处:网络
I am currently working on a project that uses CoreData and relations are using NSSet. I have currently 2 problems :

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 wro开发者_JS百科ng with this :

    [mySet setByAddingObject:info];

Thanks for the help.

mcb


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号