标签:
1 @interface MyObject : NSObject 2 + (instancetype)factoryMethodA; 3 + (id)factoryMethodB; 4 @end 5 6 @implementation MyObject 7 + (instancetype)factoryMethodA { return [[[self class] alloc] init]; } 8 + (id)factoryMethodB { return [[[self class] alloc] init]; } 9 @end 10 11 void doSomething() { 12 NSUInteger x, y; 13 14 x = [[MyObject factoryMethodA] count]; // Return type of +factoryMethodA is taken to be "MyObject *" 15 y = [[MyObject factoryMethodB] count]; // Return type of +factoryMethodB is "id" 16 }
1 @interface MyObjectSubclass : MyObjet 2 @end 3 4 void doSomethingElse() { 5 NSString *aString = [MyObjectSubclass factoryMethodA]; 6 }
1 main.m: Incompatible pointer types initializing ’NSString *’ with an expression of type ’MyObjectSubclass *’
Adopting Modern Objective-C 译文
标签:
原文地址:http://www.cnblogs.com/ljh-iOS/p/Adopting-Modern-Objective-C.html