I have learned basic data types before. However, these basic urban data types are not objects. Therefore, messages cannot be sent to them.
And sometimes they need to be processed as objects. Instead, we need to put basic data types into the collection (only objects can be stored in Cocoa,
Can not store basic data types), then we need to convert the basic type to a digital object. OC provides the data object "NSNumber"
Type "Wrap" into an object, so that we can process the basic data type.
The following is an example:
//// Main. m // FoundationDemo1 // Created by hmjiangqq on 14-1-23. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import
Int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... NSLog (@ "Hello, World! "); /* ============== NSNumber ===========================* // create an NSNumber object NSNumber * monthNumber = [[NSNumber alloc] initWithInt: 10]; NSNumber * lengthNumber = [[NSNumber alloc] initWithFloat: 10.8]; NSLog (@ "month = % @ \ n", monthNumber ); NSLog (@ "length = % @ \ n", lengthNumber); // restore to the basic data type int month = [monthNumber intValue]; float length = [lengthNumber floatValue]; NSLog (@ "month = % d \ n", month); NSLog (@ "length = %. 1f \ n ", length ); /* ================== NSString =========================================*/} return 0 ;}
The above only uses two packaging and restoration methods in NSNumber. There are many similar methods in NSNumber:
- (id)initWithChar:(char)value;- (id)initWithUnsignedChar:(unsigned char)value;- (id)initWithShort:(short)value;- (id)initWithUnsignedShort:(unsigned short)value;- (id)initWithInt:(int)value;- (id)initWithUnsignedInt:(unsigned int)value;- (id)initWithLong:(long)value;- (id)initWithUnsignedLong:(unsigned long)value;- (id)initWithLongLong:(long long)value;- (id)initWithUnsignedLongLong:(unsigned long long)value;- (id)initWithFloat:(float)value;- (id)initWithDouble:(double)value;- (id)initWithBool:(BOOL)value;- (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);- (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
The opposite method is as follows:
- (char)charValue;- (unsigned char)unsignedCharValue;- (short)shortValue;- (unsigned short)unsignedShortValue;- (int)intValue;- (unsigned int)unsignedIntValue;- (long)longValue;- (unsigned long)unsignedLongValue;- (long long)longLongValue;- (unsigned long long)unsignedLongLongValue;- (float)floatValue;- (double)doubleValue;- (BOOL)boolValue;- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);- (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);
Of course, you can also use the static method of NSNumber to create objects. The method is as follows:
+ (NSNumber *)numberWithChar:(char)value;+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;+ (NSNumber *)numberWithShort:(short)value;+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;+ (NSNumber *)numberWithInt:(int)value;+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;+ (NSNumber *)numberWithLong:(long)value;+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;+ (NSNumber *)numberWithLongLong:(long long)value;+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;+ (NSNumber *)numberWithFloat:(float)value;+ (NSNumber *)numberWithDouble:(double)value;+ (NSNumber *)numberWithBool:(BOOL)value;+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);