Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0

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


作者:wsq724439564 发表于2013-8-21 13:55:49 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>