Equality of Two NSMutableSets Using Custom Class Attributes

烈酒焚心 提交于 2019-12-25 06:42:20

问题


How do you check if two NSMutableSets are equal (same members, same number of members)?

My implementation of isEqualToSet does not seem to be working.

// members is a NSMutableSet of AUser objects

// users is also a NSMutableSet of AUser objects, it is an attribute of instances of the AGroup class

[[group valueForKey:@"users"] isEqualToSet:members]


AGroup
- users

AUser
- name  (String)

How do I check if the sets are equal by checking their name attributes?

Sorry for my lack of knowledge, it's my first time with iOS programming, so I only know the basics at the moment.


回答1:


If you want to check if the corresponding name attributes are identical, the following should work:

[[group valueForKeyPath:@"users.name"] isEqualToSet:[members valueForKey:@"name"]]

[group valueForKeyPath:@"users.name"] returns the set of the names of all users in the group, and [members valueForKey:@"name"] returns the set of the names of all users in the members set.

Update: As it became clear in the comments, members is a set of strings, and not a set of user objects. Therefore the code simplifies to:

[[group valueForKeyPath:@"users.name"] isEqualToSet:members]


来源:https://stackoverflow.com/questions/15287706/equality-of-two-nsmutablesets-using-custom-class-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!