Skip to content

Difference between Set (Swift ) and NSSet

June 7, 2015

One major difference I have observed between Set introduced as part of Swift and NSSet which is already available in Foundation framework is the way isSubSet method works in both.

NSSet does not bother the difference between a proper subset and a subset. But Set in Swift has method called

isStrictSubsetOf  in addition to isSubsetOf  which captures the scenario which Foundation fails to.

Swift Set

var set1 = Set(["r","a","m"])
var set2 = Set(["r","a","m"])

var bool = set1.isSubsetOf(set2) as Bool
var bool1 = set1.isStrictSubsetOf(set2) as Bool

 bool = set2.isStrictSubsetOf(set2) as Bool


Foundation NSSet 

NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", nil];

NSSet *set3 = [NSSet setWithObjects:@"a", @"b", @"c", nil];;

NSLog(@"isEqual :%d", [set1 isEqualToSet:set3]);

NSLog(@"isSubSet :%d", [set1 isSubsetOfSet:set1]);

For differences between Proper Subset and Subset

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: