OC 中自己的一些疑问(1)

  1. 关于 nil Nil NULL NSNull 的区别
名称 解释
nil Defines the id of a null instance
Nil Defines the id of a null class.
NSNull The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).
NULL 没找到,这是 C语言中的空指针

2.关于对象类型和相等的判断

名称 解释
isMemberOfClass Returns a Boolean value that indicates whether the receiver is an instance of a given class.
isKindOfClass Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

3.关于id 和 instancetype的区别

名称 解释
id A pointer to an instance of a class.
instancetype Using instancetype instead of id in appropriate places improves type safety in your Objective-C code

具体的区别如下:

@interface MyObject : NSObject
+ (instancetype)factoryMethodA;
+ (id)factoryMethodB;
@end

@implementation MyObject
+ (instancetype)factoryMethodA { return [[[self class] alloc] init]; }
+ (id)factoryMethodB { return [[[self class] alloc] init]; }
@end

void doSomething() {
NSUInteger x, y;

x = [[MyObject factoryMethodA] count]; // Return type of +factoryMethodA is taken to be "MyObject *"
y = [[MyObject factoryMethodB] count]; // Return type of +factoryMethodB is "id"
}

Because of the instancetype return type of +factoryMethodA, the type of that message expression is MyObject *. Since MyObject doesn’t have a -count method, the compiler gives a warning about the x line:
main.m: ’MyObject’ may not respond to ‘count’

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容