Wednesday, April 30, 2014

iOS Tutorial - Part 9 - NSNumber, id Type

NSNumber, id Type

Video Description 
 

NSNumber

It defines a set of methods specifically for setting and accessing the value.
@property (nonatomic, strong) NSNumber *myNumber;
numberWithInt
A wrapper around int to convert it to an object.
int myInt = 2;
self.myNumber = [NSNumber numberWithInt:myInt]; 
NSNumber has other useful methods for the other primitive types. Methods like: numberWithDouble, numberWithFloat, numberWithBool, numberWithChar, ...
Primitive types
Here are the important primitive types in Objective-C: int, BOOL, float, char, double, short, long.
stringValue
You can convert int type into string by using NSNumber method.
NSString *myStrig = [self.myNumber stringValue];
We have some similar methods like floatValue, intValue, boolValue, shortValue, doubleValue that can convert primitives into each other.

Type id

When we don't know the type of an object we use id as a type. It is useful when we want to assign a variable to a type but we are not sure what kind of object we receive (We will have more examples in the future).
@property (nonatomic, strong) id UnknownP;

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];