Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • NSSet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 834
    Comment on it

    An NSSet object represents a static, unordered collection of distinct objects. NSSet/NSMutableSet, NSArray/NSMutableArray, and NSDictionary/NSMutableDictionary are the three core collection classes .

    NSSet is immutable and NSMutableSet is mutable or we can say that a NSMutableSet can be modified(Add/Remove) where NSSet can't be.

    Initialize a NSSet and NSMutableSet

    NSSet *carManufacturers = [NSSet setWithObjects:@"Chrysler", @"Ford",
                                                 @"General Motors", nil];
    NSLog(@"%@", carManufacturers);
    
    NSMutableSet *carManufacturers = [NSMutableSet setWithObjects:
                                @"Honda Civic", @"Nissan Versa", nil];
    NSMutableSet *topCars = [NSMutableSet setWithCapacity:5];
    

    Compare NSSet

    NSSet *cars = [NSSet setWithObjects:@"Honda", @"Nissan",
                                  @"Mitsubishi", @"Toyota", nil];
    NSSet *myFav = [NSSet setWithObjects:@"Honda", nil];
    NSSet *allTimeFav = [NSSet setWithObjects:@"Toyota",
                                                      @"Alfa Romeo", nil];
    
    if ([myFav isEqualToSet:cars]) {
        NSLog(@"I like all the Japanese auto makers and no others");
    }
    if ([myFav intersectsSet:cars]) {
        // You'll see this message
        NSLog(@"I like at least one Japanese auto maker");
    }
    if ([myFav isSubsetOfSet:cars]) {
        // And this one, too
        NSLog(@"All of the auto makers that I like are Japanese");
    }
    if ([allTimeFav isSubsetOfSet:cars]) {
        NSLog(@"All of the auto makers that people likes are Japanese");
    }
    

    Find an Object in NSSet check if an object is in a particular NSSet.

    NSSet *carMakers = [NSSet setWithObjects:@"Maserati",
                                                 @"Porsche", nil];
    // BOOL checking
    if ([carMakers containsObject:@"Maserati"]) {
        NSLog(@"The user seems to like expensive cars");
    }
    // nil checking
    NSString *result = [carMakers member:@"Maserati"];
    if (result != nil) {
        NSLog(@"%@ is one of the selected makes", result);
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: