2013-08-21

在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objective-C提供了一个 NSNumber

类来作为一个中转,可以将所有的普通数据类型转化为NSNumber类型,这样就符合Objective-C的消息机制了。

NSNumber 
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{ @autoreleasepool { //初始化NSNumber使用格式: numberWith+object
NSNumber * intNumber=[NSNumber numberWithInteger:100];
//跳到NSInter定义的地方可以看到:typedef long NSInteger; NSInteger 其实就是long类型,只不过是重新定义了一个名字罢了。
// 取intNumber的值使用格式: object+Value
NSInteger dwint=[intNumber integerValue];
NSLog(@"%ld",dwint);
}
return 0;
}
结果:
2013-08-21 11:42:07.151 NSNumber[492:707] 100

 

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{ @autoreleasepool { // numberWith+object
NSNumber * intNumber=[NSNumber numberWithDouble:12345e+];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
} 结果:
2013-08-21 11:34:23.631 NSNumber[435:707] -9223372036854775808
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{ @autoreleasepool { // numberWith+object
NSNumber * intNumber=[NSNumber numberWithDouble:12345e+];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
}
结果:
2013-08-21 11:36:19.330 NSNumber[451:707] 123450000000000
 

给已经初始化的NSNumber对象重新赋值,也就是改变他指向空间里面的内容,只能通过给他重新指向来改变它的值,不能改变它最开始指向空间里面的值,因为产生的是一个常量,

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{ @autoreleasepool { NSNumber * intNumber=[NSNumber numberWithInteger:];
NSInteger dwint=[intNumber integerValue];
NSLog(@"%ld",dwint);
//NSLog(@"%d",[intNumber retainCount]); const
//[intNumber initWithInteger:100]; error ,cannot be sent to an abstract object of class 
intNumber=[NSNumber numberWithInteger:];
NSLog(@"%ld",[intNumber integerValue]);
}
return ;
}
结果:

2013-08-21 13:43:39.382 NSNumber[556:707] 100
2013-08-21 13:43:39.389 NSNumber[556:707] 1000

 

附上NSNumber类的原型:

 /*    NSValue.h
Copyright (c) 1994-2011, Apple Inc. All rights reserved.
*/ #import <Foundation/NSObject.h> @class NSString, NSDictionary; @interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value;
- (const char *)objCType; @end @interface NSValue (NSValueCreation) - (id)initWithBytes:(const void *)value objCType:(const char *)type;
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
+ (NSValue *)value:(const void *)value withObjCType:(const char *)type; @end @interface NSValue (NSValueExtensionMethods) + (NSValue *)valueWithNonretainedObject:(id)anObject;
- (id)nonretainedObjectValue; + (NSValue *)valueWithPointer:(const void *)pointer;
- (void *)pointerValue; - (BOOL)isEqualToValue:(NSValue *)value; @end

//开始================================================================================
@interface NSNumber : NSValue - (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); - (NSString *)stringValue; - (NSComparisonResult)compare:(NSNumber *)otherNumber; - (BOOL)isEqualToNumber:(NSNumber *)number; - (NSString *)descriptionWithLocale:(id)locale; @end @interface NSNumber (NSNumberCreation) - (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); + (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); @end
04-18 16:05