- 关于 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’