Clev3r
Clev3r

Reputation: 1588

If any element in setA is in setB

Just a quick question regarding sets in Obj-c. Given two sets:

NSMutableSet* a = [NSMutableSet setWithObjects: 1, 2, 3, nil];
NSMutableSet* b = [NSMutableSet setWithObjects: 3, 4, 5, nil];

is there a quick and easy way to determine if any element in set A is also in set B?

Something like ...

if ([a contains:[b allObjects]])
   // do something

Upvotes: 0

Views: 125

Answers (2)

Catfish_Man
Catfish_Man

Reputation: 41811

The word you're looking for is "intersect" :)

if ([a intersectsSet:b]) {
    ...
}

Upvotes: 1

Lily Ballard
Lily Ballard

Reputation: 185681

This is what -intersectsSet: is for.

if ([a intersectsSet:b])
    // do something

Upvotes: 2

Related Questions