1.平台相关的数据类型

These types, like NSInteger and 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,NSSet and NSDictionary are 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 NSNumber or NSValue instance to represent it.

  集合类型的元素必须是OC对象,而不能是标量值

  所谓标量值,是指int,double等基本数据类型和NSInteger,NSUInteger,BOOL等平台相关数据类型。标量值,一般声明的时候不加星号,比如NSInteger a = 1;矢量值,一般声明的时候需要加星号,比如NSNumber *b = @1;

5.集合快速初始化的时候有个坑

If you do need to represent a nil value in one of the collection classes, you should use the NSNull singleton class

  Collection类,如NSArray,NSSet,NSDictionary等都是nil-terminated,所以使用类似下例的方式初始化的时候,注意一定要检查元素是否是nil啊

//事件上报 Norcy
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: _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", @, [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 = ;
for (id obj in [array objectEnumerator]) //正向
{
NSLog(@"%@", obj);
index++;
}

  

《Programming with Objective-C》第七章 Values and Collections的更多相关文章

  1. Programming In Scala笔记-第七章、Scala中的控制结构

    所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...

  2. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...

  3. [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第二章~第七章

    在看完<Objective-C 程序设计 第6版>之后,看了一些关于iOS开发职位的面试题,发现自身基础非常是不牢,于是打算以查缺补漏的方式阅读还有一本关于Objective-C的基础书籍 ...

  4. [Effective Java]第七章 方法

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. Linux Shell 示例精解(第七章 gawk编程)转载

    第七章 gawk功能:gawk编程 7.1.1 数字和字符串常量     初始化和类型强制  在awk中,变量不需要定义就可以直接使用,使用一个变量就是对变量的定义.变量的类型可以试数字.字符串,或者 ...

  6. 第七章 DAO模式

    第七章 DAO模式 一.JDBC的封装 1.JDBC的封装: DAO位于业务逻辑和持久化数据之间,实现对持久化数据的访问.将数据库都封装起来,对外提供相应的接口 2.DAO模式的作用: 1.隔离业务逻 ...

  7. Android群英传笔记——第七章:Android动画机制和使用技巧

    Android群英传笔记--第七章:Android动画机制和使用技巧 想来,最 近忙的不可开交,都把看书给冷落了,还有好几本没有看完呢,速度得加快了 今天看了第七章,Android动画效果一直是人家中 ...

  8. 2018-11-27 中文代码示例之Programming in Scala笔记第七八章

    续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...

  9. 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记

    注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...

随机推荐

  1. websphere设置jvm参数

    http://www.quiee.com.cn/archives/592/ websphere 选择服务器-> 应用程序服务器-> Server1-> 进程定义->Java 虚 ...

  2. git eclipse 不标记修改后的文件(没有图标标明)

    在使用Eclipse做开发的时候,已经修改了某个文件,但是文件的图标没有明显的标示,如图: 解决上面问题的办法如下:

  3. 设置 TIM3_CH2 的 PWM 模式,使能 TIM3 的 CH2 输出

    /**   ******************************************************************************   * @file    st ...

  4. RPC服务框架dubbo(一):简介和原理解析

    前置概念 在学习dubbo前,需要先了解SOA和RPC这两个概念. SOA 1.英文名称(Service Oriented Ambiguity) 2.中文名称:面向服务架构 2.1 有一个专门提供服务 ...

  5. mysql远程登录出错的解决方法

    mysql远程登录出错的情况,先比很多朋友都有遇到过吧,下面有个不错的解决方法,大家可以参考下. 错误:ERROR 2003 (HY000): Can't connect to MySQL serve ...

  6. Atitit.国际化中的日期与钱符号问题

    Atitit.国际化中的日期与钱符号问题 1. 用户名注册的问题 1 1.1. 不能限制用户名长度与特殊符号 1 2. 2.8月7号未必总写成8/7,有时也用7/8 2 3. 5.$未必总是指美元 3 ...

  7. pcie dma的玩法

    There is some issue with the implement script. So I took the manual steps. 1. Created the pcie core ...

  8. iPhone How-to:如何调整UIView的Z-Order

    转自:http://bj007.blog.51cto.com/1701577/541572 在界面设计中,最终用户看到的呈现通常是由不同层的视图组成的,通过控制视图的层次就可以实现不同的效果和功能.而 ...

  9. CBiontCache

    /************************************************************************/ /* 预先加载一些生物以备将来使用 */ /* 专 ...

  10. MySQL是如何做到安全登陆

    首先Mysql的密码权限存储在mysql.user表中.我们不关注鉴权的部分,我们只关心身份认证,识别身份,后面的权限控制是很简单的事情.在mysql.user表中有个authentication_s ...