ios developer tiny share-20161014
来源:互联网 发布:java获取svn文件列表 编辑:程序博客网 时间:2024/06/07 11:06
今天讲ios的NSNumber,以及NSUInteger
Numbers Are Represented by Instances of the NSNumber Class
The NSNumber class is used to represent any of the basic C scalar types, including char, double, float, int, long, short, and the unsigned variants of each, as well as the Objective-C Boolean type, BOOL.As with NSString, you have a variety of options to create NSNumber instances, including allocation and initialization or the class factory methods:
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];NSNumber *unsignedNumber = [[NSNumber alloc] initWithUnsignedInt:42u];NSNumber *longNumber = [[NSNumber alloc] initWithLong:42l];NSNumber *boolNumber = [[NSNumber alloc] initWithBOOL:YES];NSNumber *simpleFloat = [NSNumber numberWithFloat:3.14f];NSNumber *betterDouble = [NSNumber numberWithDouble:3.1415926535];NSNumber *someChar = [NSNumber numberWithChar:'T'];
It’s also possible to create NSNumber instances using Objective-C literal syntax:
NSNumber *magicNumber = @42;NSNumber *unsignedNumber = @42u;NSNumber *longNumber = @42l;NSNumber *boolNumber = @YES;NSNumber *simpleFloat = @3.14f;NSNumber *betterDouble = @3.1415926535;NSNumber *someChar = @'T';
These examples are equivalent to using the NSNumber class factory methods.
Once you’ve created an NSNumber instance it’s possible to request the scalar value using one of the accessor methods:
int scalarMagic = [magicNumber intValue];unsigned int scalarUnsigned = [unsignedNumber unsignedIntValue];long scalarLong = [longNumber longValue];BOOL scalarBool = [boolNumber boolValue];float scalarSimpleFloat = [simpleFloat floatValue];double scalarBetterDouble = [betterDouble doubleValue];char scalarChar = [someChar charValue];
The NSNumber class also offers methods to work with the additional Objective-C primitive types. If you need to create an object representation of the scalar NSInteger and NSUInteger types, for example, make sure you use the correct methods:
NSInteger anInteger = 64;NSUInteger anUnsignedInteger = 100;NSNumber *firstInteger = [[NSNumber alloc] initWithInteger:anInteger];NSNumber *secondInteger = [NSNumber numberWithUnsignedInteger:anUnsignedInteger];NSInteger integerCheck = [firstInteger integerValue];NSUInteger unsignedCheck = [secondInteger unsignedIntegerValue];
All NSNumber instances are immutable, and there is no mutable subclass; if you need a different number, simply use another NSNumber instance.
Note: NSNumber is actually a class cluster. This means that when you create an instance at runtime, you’ll get a suitable concrete subclass to hold the provided value. Just treat the created object as an instance of NSNumber.
0 0
- ios developer tiny share-20161014
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- codeforces 295B Greg and Graph [Floyed]
- poj2155 二维树状数组 二位线段树 灵活运用 待补完
- Ubuntu--(7)现有轮子
- opencv 仿射变换 根据眼睛坐标进行人脸对齐 计算变换后对应坐标
- 利用Chrome浏览器调试Android数据库
- ios developer tiny share-20161014
- Flutter基础—第一个Flutter实例
- boost :: scoped_ptr的和std ::的unique_ptr的区别
- 第2.1.4章 WEB系统最佳实践Spring文件配置之spring-hessian-server.xml
- C++Virus手打系列,仅供研究
- 图像处理(二)opencv处理图像二值化,灰度化等
- 第二次作业——Service、Listview与ACtivity参数传递
- spring声明式事务管理
- jquery源码——buildFragment