《Programming with Objective-C》第七章 Values and Collections
2015-10-14 22:28
555 查看
1.平台相关的数据类型
These types, like
尽量使用这种平台相关的数据类型,而不是写int,double,float等基本类型,防止在不同的系统平台上,基本数据类型溢出的问题
2.使用int还是NSInteger
For local variables, such as a counter in a loop, it’s fine to use the basic C types if you know that the value is within the standard limits.
写for循环的时候,用int i = 0; i < n; i++ 吧,因为你知道i会不会溢出
3.NSNumber
使用NSNumber来表示基本数据类型
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'];
使用NSNumber来表示基本数据类型之更简洁的方法
NSNumber *magicNumber = @42;
NSNumber *unsignedNumber = @42u;
NSNumber *longNumber = @42l;
NSNumber *boolNumber = @YES;
NSNumber *simpleFloat = @3.14f;
NSNumber *betterDouble = @3.1415926535;
NSNumber *someChar = @'T';
从NSNumber里面取出基本数据类型
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];
NSNumber和NSInteger之间的转化
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];
4.集合类型的元素必须是OC对象
If you need to add a scalar value, you must first create a suitable
集合类型的元素必须是OC对象,而不能是标量值
所谓标量值,是指int,double等基本数据类型和NSInteger,NSUInteger,BOOL等平台相关数据类型。标量值,一般声明的时候不加星号,比如NSInteger a = 1;矢量值,一般声明的时候需要加星号,比如NSNumber *b = @1;
5.集合快速初始化的时候有个坑
If you do need to represent a
Collection类,如NSArray,NSSet,NSDictionary等都是nil-terminated,所以使用类似下例的方式初始化的时候,注意一定要检查元素是否是nil啊
不然中间哪个元素是nil的时候,后面的元素就添加不上去了,那真的要表示nil的时候怎么办呢,除了上例的NSString使用了@""之外,还可以用NSNull这个类来代替nil
由于NSNull是一个单例,[NSNull null]返回的永远是同一个对象,所以可以使用以下方法判断Collection里面哪个是空的
6.快速枚举
数组之快速枚举
字典之快速枚举(注意是key哦)
高大上的NSEnumerator
如果,你离不开for循环的下标
These types, like
NSIntegerand
NSUInteger, are defined differently depending on the target architecture. When building for a 32-bit environment (such as for iOS), they are 32-bit signed and unsigned integers respectively; when building for a 64-bit environment (such as for the modern OS X runtime) they are 64-bit signed and unsigned integers respectively.
尽量使用这种平台相关的数据类型,而不是写int,double,float等基本类型,防止在不同的系统平台上,基本数据类型溢出的问题
2.使用int还是NSInteger
For local variables, such as a counter in a loop, it’s fine to use the basic C types if you know that the value is within the standard limits.
写for循环的时候,用int i = 0; i < n; i++ 吧,因为你知道i会不会溢出
3.NSNumber
使用NSNumber来表示基本数据类型
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'];
使用NSNumber来表示基本数据类型之更简洁的方法
NSNumber *magicNumber = @42;
NSNumber *unsignedNumber = @42u;
NSNumber *longNumber = @42l;
NSNumber *boolNumber = @YES;
NSNumber *simpleFloat = @3.14f;
NSNumber *betterDouble = @3.1415926535;
NSNumber *someChar = @'T';
从NSNumber里面取出基本数据类型
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];
NSNumber和NSInteger之间的转化
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];
4.集合类型的元素必须是OC对象
NSArray,
NSSetand
NSDictionaryare used to manage groups of objects, which means any item you wish to add to a collection must be an instance of an Objective-C class.
If you need to add a scalar value, you must first create a suitable
NSNumberor
NSValueinstance to represent it.
集合类型的元素必须是OC对象,而不能是标量值
所谓标量值,是指int,double等基本数据类型和NSInteger,NSUInteger,BOOL等平台相关数据类型。标量值,一般声明的时候不加星号,比如NSInteger a = 1;矢量值,一般声明的时候需要加星号,比如NSNumber *b = @1;
5.集合快速初始化的时候有个坑
If you do need to represent a
nilvalue in one of the collection classes, you should use the
NSNullsingleton class
Collection类,如NSArray,NSSet,NSDictionary等都是nil-terminated,所以使用类似下例的方式初始化的时候,注意一定要检查元素是否是nil啊
//事件上报 Norcy
NSDictionary*dic = [NSDictionarydictionaryWithObjectsAndKeys: _resultModel.keyWord?_resultModel.keyWord:@"", @"searchKeyword", nil]; //这里要检查是否是nil
[QLReportCtlMgr reportStrID:@"video_jce_show_search_result_page" params:dic];
不然中间哪个元素是nil的时候,后面的元素就添加不上去了,那真的要表示nil的时候怎么办呢,除了上例的NSString使用了@""之外,还可以用NSNull这个类来代替nil
NSArray *array = @[ @"string", @42, [NSNull null], @YES];
由于NSNull是一个单例,[NSNull null]返回的永远是同一个对象,所以可以使用以下方法判断Collection里面哪个是空的
for (id object in array) { if (object == [NSNull null]) NSLog(@"Found a null object"); }
6.快速枚举
数组之快速枚举
for (id eachObject in array) { NSLog(@"Object: %@", eachObject); }
字典之快速枚举(注意是key哦)
for (NSString *eachKey in dictionary) { id object = dictionary[eachKey]; NSLog(@"Object: %@ for key: %@", object, eachKey); }
高大上的NSEnumerator
for (id obj in [array objectEnumerator]) //正向 { NSLog(@"%@", obj); } for (id obj in [array reverseObjectEnumerator]) //反向 { NSLog(@"%@", obj); } while (id obj = [enumerator nextObject]) //nextObject+while { NSLog(@"%@", obj); }
如果,你离不开for循环的下标
int index = 0; for (id obj in [array objectEnumerator]) //正向 { NSLog(@"%@", obj); index++; }
相关文章推荐
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-04-目标函数
- Objective-C:KVO机制
- geodatabase中如何获取feature class的ObjectID
- Android数据解析获取JSONObject的key值-Iterator应用
- Objective-C:KVC机制
- How to prevent object bloat in PostgreSQL
- objective-c制作汤姆猫
- Object - C的知识总结->方法
- iOS/object-c: 枚举类型 enum,NS_ENUM,NS_OPTIONS
- Objective—C常见特性(instancetype和id比较、@property、枚举宏(Enumeration Macros))
- Caused by: java.lang.ClassNotFoundException: com.alibaba.fastjson.JSONObject
- IOS 开发学习(1): 学习纲领以及Objective-C 基本语法学习
- MacTex XeLaTex xdvipdfmx:fatal: pdf_ref_obj(): passed invalid object. 报错的解决方法
- Objective-C的singleton模式
- Objective-C Runtime 运行时之四:Method Swizzling
- 面试题:为什么其他语言里叫函数调用,objective-c里则是给对象发消息(或者谈下对runtime的理解)
- Objective-C Runtime 运行时之三:方法与消息
- 2015 Objective-C 新特性
- Objective-C 中类方法load 和 类方法 initialize的区别
- iOS objective-c中数字转为罗马数字