#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSNumber *aBool=[NSNumber numberWithBool:NO];
NSNumber *aChar=[NSNumber numberWithChar:'z'];
NSNumber *anInt=[NSNumber numberWithInt:234545];
NSNumber *aDouble=[NSNumber numberWithDouble:34.345];
NSLog(@ "%@",[aBool boolValue]?@ "YES":@ "NO");
NSLog(@ "%c",[aChar charValue]);
NSLog(@ "%lu",[anInt integerValue]);
NSLog(@ "%f",[aDouble doubleValue]);
//Assigning direct literals is valid.
aBool=@ YES;
NSLog(@ "%@",[aBool boolValue]?@ "YES":@ "NO");
//NSNumber could be used directly with any data type.
float x=5;
NSNumber *result=@(x*10);
NSLog(@ "%@",result);
//could also be used in counter to store value at runtime.
NSNumber *counter=@ 0;
for (int i=0; i<10; i++) {
counter=@ ([counter integerValue]+1);
}
NSLog(@ "%@",counter);
//Use of NSNumber with NSComparisonResult
NSNumber *anInteger=@ 65;
NSNumber *anotherInt=@ 42;
NSComparisonResult result1 = [anInteger compare:anotherInt];
if (result1 == NSOrderedAscending) { //It will return 1 if true.
NSLog(@ "65<42");
}
else if (result1 == NSOrderedSame){ //It will return 0 if true.
NSLog(@ "65==42");
}
else if (result1 == NSOrderedDescending){ //It will return -1 if true.
NSLog(@ "65>42");
}
}
return 0;
}
No comments:
Post a Comment