Pages

Wednesday, December 30, 2009

Objective-C. NSNumber

The numeric data types in Objective-C can be objects. So we can send a message to them (sounds strange for someone who began programming from C). One more strange reason to have the numbers as the objects is a possibility to store them in NSArray (trivial malloc works in Objective-C, so why I need an array of objects to save the integer values?).

Anyway, even if I do not understand, it exists:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSValue.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSNumber* number;

number = [NSNumber numberWithInteger:112];
NSLog(@"integer %@", number);

number = [NSNumber numberWithFloat:123.45];
NSLog(@"float %@", number);

[pool drain];
return 0;
}

No comments:

Post a Comment