• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C,ios,iphone开发基础:几个常用类-NSNumber

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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+15];
        NSLog(@"%ld",[intNumber integerValue]);
    }
    return 0;
}

结果:
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+10];
        NSLog(@"%ld",[intNumber integerValue]);
    }
    return 0;
}


结果:
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:100];
        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:1000];
        NSLog(@"%ld",[intNumber integerValue]);
    }
    return 0;
}
结果:

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

 

附上NSNumber类的原型:

 1 /*    NSValue.h
 2     Copyright (c) 1994-2011, Apple Inc. All rights reserved.
 3 */
 4 
 5 #import <Foundation/NSObject.h>
 6 
 7 @class NSString, NSDictionary;
 8 
 9 @interface NSValue : NSObject <NSCopying, NSCoding>
10 
11 - (void)getValue:(void *)value;
12 - (const char *)objCType;
13 
14 @end
15 
16 @interface NSValue (NSValueCreation)
17 
18 - (id)initWithBytes:(const void *)value objCType:(const char *)type;
19 + (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
20 + (NSValue *)value:(const void *)value withObjCType:(const char *)type;
21 
22 @end
23 
24 @interface NSValue (NSValueExtensionMethods)
25 
26 + (NSValue *)valueWithNonretainedObject:(id)anObject;
27 - (id)nonretainedObjectValue;
28 
29 + (NSValue *)valueWithPointer:(const void *)pointer;
30 - (void *)pointerValue;
31 
32 - (BOOL)isEqualToValue:(NSValue *)value;
33 
34 @end
35 
//开始================================================================================ 36 @interface NSNumber : NSValue 37 38 - (char)charValue; 39 - (unsigned char)unsignedCharValue; 40 - (short)shortValue; 41 - (unsigned short)unsignedShortValue; 42 - (int)intValue; 43 - (unsigned int)unsignedIntValue; 44 - (long)longValue; 45 - (unsigned long)unsignedLongValue; 46 - (long long)longLongValue; 47 - (unsigned long long)unsignedLongLongValue; 48 - (float)floatValue; 49 - (double)doubleValue; 50 - (BOOL)boolValue; 51 - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0); 52 - (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0); 53 54 - (NSString *)stringValue; 55 56 - (NSComparisonResult)compare:(NSNumber *)otherNumber; 57 58 - (BOOL)isEqualToNumber:(NSNumber *)number; 59 60 - (NSString *)descriptionWithLocale:(id)locale; 61 62 @end 63 64 @interface NSNumber (NSNumberCreation) 65 66 - (id)initWithChar:(char)value; 67 - (id)initWithUnsignedChar:(unsigned char)value; 68 - (id)initWithShort:(short)value; 69 - (id)initWithUnsignedShort:(unsigned short)value; 70 - (id)initWithInt:(int)value; 71 - (id)initWithUnsignedInt:(unsigned int)value; 72 - (id)initWithLong:(long)value; 73 - (id)initWithUnsignedLong:(unsigned long)value; 74 - (id)initWithLongLong:(long long)value; 75 - (id)initWithUnsignedLongLong:(unsigned long long)value; 76 - (id)initWithFloat:(float)value; 77 - (id)initWithDouble:(double)value; 78 - (id)initWithBool:(BOOL)value; 79 - (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0); 80 - (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0); 81 82 + (NSNumber *)numberWithChar:(char)value; 83 + (NSNumber *)numberWithUnsignedChar:(unsigned char)value; 84 + (NSNumber *)numberWithShort:(short)value; 85 + (NSNumber *)numberWithUnsignedShort:(unsigned short)value; 86 + (NSNumber *)numberWithInt:(int)value; 87 + (NSNumber *)numberWithUnsignedInt:(unsigned int)value; 88 + (NSNumber *)numberWithLong:(long)value; 89 + (NSNumber *)numberWithUnsignedLong:(unsigned long)value; 90 + (NSNumber *)numberWithLongLong:(long long)value; 91 + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; 92 + (NSNumber *)numberWithFloat:(float)value; 93 + (NSNumber *)numberWithDouble:(double)value; 94 + (NSNumber *)numberWithBool:(BOOL)value; 95 + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0); 96 + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0); 97 98 @end

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
(iphone)Objectivechessian发布时间:2022-07-12
下一篇:
ObjectiveC绘制透明窗口的方法发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap