ios问题集

1.nil,NULL,NSNull的区别

nil: A null pointer to an Objective-C object.
( #define nil ((id)0)  )  
Nil: A null pointer to an Objective-C class.       

NULL: A null pointer to anything else. 
( #define NULL ((void *)0)  ) 

NSNull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values).

[NSNull null]: The singleton instance of NSNull.
Technically they're all the same,,, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.



If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability;

 

nil:指向oc中对象的空指针

Nil:指向oc中类的空指针

NULL:指向其他类型的空指针,如一个c类型的内存指针

NSNull:在集合对象中,表示空值的对象

若obj为nil:

[obj message]将返回NO,而不是NSException

若obj为NSNull:

[obj message]将抛出异常NSException

 参考http://blog.sina.com.cn/s/blog_5fb39f910101akm1.html

 

2.#import与@class的区别

 

 

 "import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告诉你。

 

   在头文件中, 一般只需要知道被引用的类的名称就可以了。 不需要知道其内部的实体变量和方法,所以在头文件中一般使用@class来声明这个名称是类的名称。 而在实现类里面,因为会用到这个引用类的内部的实体变量和方法,所以需要使用#import来包含这个被引用类的头文件。

 

   在编译效率方面考虑,如果你有100个头文件都#import了同一个头文件,或者这些文件是依次引用的,如A–>B, B–>C, C–>D这样的引用关系。当最开始的那个头文件有变化的话,后面所有引用它的类都需要重新编译,如果你的类有很多的话,这将耗费大量的时间。而是用 @class则不会。

  简单的说,在头文件里用@class,在执行文件#import,是加快编译速度。" 

参考:http://www.cocoachina.com/bbs/read.php?tid=734&page=2

http://blog.csdn.net/wildcatlele/article/details/9446279

http://www.cnblogs.com/jqyp/archive/2012/01/13/2321707.html

 

posted on 2013-12-23 21:59  Satch.mo  阅读(150)  评论(0编辑  收藏  举报

导航