开发者

NSPredicates with custom objects

开发者 https://www.devze.com 2022-12-25 23:21 出处:网络
I have the following problem and I cannot figure out how to solve it. I have an NSSet which contains Person objects (NSDictionary with 2 keys: name, age).开发者_如何转开发 From time to time I get an N

I have the following problem and I cannot figure out how to solve it. I have an NSSet which contains Person objects (NSDictionary with 2 keys: name, age).开发者_如何转开发 From time to time I get an NSArray of Person objects and I want to add them to my NSSet but I want to filter out the duplicates. I'm thinking I could use NSPredicates but I am reading the documentation and I can't understand how should I format my NSPredicate to filter out my array content based on what I already have in NSSet.

So I have an NSSet with 5 custom objects and I get an NSArray with 6 objects but 3 of them already exist in NSSet and I want to filter them out. I understand what I should do with basic objects like Strings, integers, etc but I don't know how to extend this to work with custom more complex objects.

I hope I was clear enough with my problem. Any starting point, example or advice is much appreciated.

Thank you!


If you already have a mutable set, you don't really have to do anything special. If you get the same Person instance back at some point in the future, addObject: will simply ignore it. If you have a different meaning of "equality" than just whether two objects are the same instance, then you have to deal with that yourself, but if your new object returns YES for isEqual: then addObject: will already ignore it as well when you place it in the set.

0

精彩评论

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

关注公众号