Sunday, April 13, 2014

iOS Tutorial - Part 8 - Local Variable, NSDictionary, NSMutableDictionary

Local Variables, NSDictionary, NSMutableDictionary


Video Description 

Local Variables

You can define a local variable and in the same line you can also initialize it by allocating memory for it like bellow:
NSArray *myArray = [[NSArray alloc] init];
NSString *myLocalString = [[NSString alloc] init];

Useful methods of NSDictionary

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. We can declare and initialize a dictionary with keys and values with this method dictionaryWithObjectsAndKeys
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"value0", @"key0",
                            @"value1", @"key1",
                            @"value2", @"key2",
                            nil];

count
Returns the number of keys or values (number of keys and values are equal)
NSLog(@" %d", myDict.count);

objectForKey:key
Returns the value for the specified key
NSLog(@" %@", [myDict valueForKey:@"value0"]);
allKeys
Returns an array containing all keys in the dictionary
NSLog(@" %@", [myDict allkeys]);
allValues
Returns an array containing all values in the dictionary
NSLog(@" %@", [myDict allValues]);

Useful methods of NSMutableDictionary

setObject:anObject forKey:key
Adds an object with specified key to the dictionary
[myMuteDict setObject:@"Apple" forKey:@"ios"];
[myMuteDict setObject:@"Google" forKey:@"android"];

removeObjectForKey:key
Removes an object that has the specified key
[myMuteDict removeObjectForKey:@"android"];

removeAllObjects
Removes all of the objects from the dictionary
[myMuteDict removeAllObjects];

No comments:

Post a Comment