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;

No comments:

Post a Comment