【发布时间】:2014-12-19 12:31:07
【问题描述】:
我对在 Objective-C 中使用 instancetype 感到困惑。
代码:
@interface MyClass : NSObject
-(instancetype)initWithOwner:(NSString*)anOwner;
@property (nonatomic, strong) NSString* owner;
@end
@interface MyChildClass : MyClass
-(instancetype)init;
@property (nonatomic, strong) NSString* myString;
@end
然后,我可以运行它
MyChildClass* myChildObject = [[MyChildClass alloc] initWithOwner:@"owner"];
为什么这会返回 MyChildClass 而不是 MyClass ???
为什么编译器不显示任何警告或错误消息,说明我使用的是超类的初始化方法,而不是 MyChildClass 初始化方法???
我在哪里可以使用这个???
【问题讨论】:
标签: objective-c inheritance factory factory-method instancetype