Search code examples
objective-ccocoa-touchcocoaequalitynsmutableset

Equality of Two NSMutableSets Using Custom Class Attributes


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.


Solution

  • 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]