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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 297
    Comment on it

    NSDictionary class represents an unordered collection of objects associate each value with a key.

    Create NSDictionary

        NSDictionary *cars = @{
        @"Mercedes" : @"Benz SLK250"],
        @"BMW M3 Coupe" : @"M3 Coupe",
        @"Honda" : @"Civic",
    };
    NSLog(@"Dictionary: %@", cars);
    NSLog(@"Manufacturers: %@", [cars allKeys]);
    NSLog(@"Models: %@", [cars allValues]);
    

    Enumerate NSDictionary

    [cars enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"There are %@ %@'s in stock", obj, key);
    }];
    

    Compare NSDictionaries

     NSDictionary *warehouse = @{
        @"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
        @"BMW X6" : [NSNumber numberWithInt:16],
    };
    NSDictionary *showroom = @{
        @"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
        @"BMW X6" : [NSNumber numberWithInt:16],
    };
    if ([warehouseInventory isEqualToDictionary:showroomInventory]) {
        NSLog(@"Why are we storing same model cars ?");
    }
    
    

    Sorting Dictionary Keys

    NSDictionary *cars = @{
        @"Mercedes" : @"Benz SLK250"],
        @"BMW M3 Coupe" : @"M3 Coupe",
        @"Honda" : @"Civic",
    };
    NSArray *sortedKeys = [cars keysSortedByValueUsingComparator:
                           ^NSComparisonResult(id obj1, id obj2) {
                               return [obj2 compare:obj1];
                           }];
    NSLog(@"%@", sortedKeys);
    

    Create a NSMutableDictionary

    NSMutableDictionary *owners = [NSMutableDictionary
                                 dictionaryWithDictionary:@{
                                     @"Audi TT" : @"John",
                                     @"Audi Quattro (Black)" : @"Mary",
                                     @"Audi Quattro (Silver)" : @"Bill",
                                     @"Audi A7" : @"Bill"
                                 }];
    NSLog(@"%@", owners);
    

    Add/Remove from a NSMutableDictionary

    NSMutableDictionary *carJobs = [NSMutableDictionary
                                 dictionaryWithDictionary:@{
                                     @"Audi TT" : @"John",
                                     @"Audi Quattro (Black)" : @"Mary",
                                     @"Audi Quattro (Silver)" : @"Bill",
                                     @"Audi A7" : @"Bill"
                                 }];
    // Transfer an existing job to Mary
    [carJobs setObject:@"Mary" forKey:@"Audi TT"];
    // Finish a job
    [carJobs removeObjectForKey:@"Audi A7"];
    // Add a new job
    carJobs[@"Audi R8 GT"] = @"Jack";
    

    Combine NSMutableDictionary

    NSMutableDictionary *carJobs = [NSMutableDictionary
                                 dictionaryWithDictionary:@{
                                     @"Audi TT" : @"John",
                                     @"Audi Quattro (Black)" : @"Mary",
                                     @"Audi Quattro (Silver)" : @"Bill",
                                     @"Audi A7" : @"Bill"
                                 }];
    
    NSDictionary *carJobsSeekers = @{
        @"BMW 640i" : @"Dick",
        @"BMW X5" : @"Brad"
    };
    [carJobs addEntriesFromDictionary:carJobsSeekers];
    

    *Happy Coding :)

 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: