I\'m trying to add a convenience constructor to my custom object.
Similar to [NSArray arrayWithArray:]
I know it involves a class method that returns an au
Let's say you have the following:
@class PotatoPeeler : NSObject
- (instancetype)initWithWidget: (Widget *)w;
@end
Then to add a factory method, you'd change it to this:
@class PotatoPeeler : NSObject
+ (instancetype)potatoPeelerWithWidget: (Widget *)w;
- (instancetype)initWithWidget: (Widget *)w;
@end
And your implementation would simply be:
+ (instancetype)potatoPeelerWithWidget: (Widget *)w {
return [[[self alloc] initWithWidget: w] autorelease];
}
Edit: replaced id
with instancetype
. They are functionally identical, but the latter provides better hints to the compiler about the method's return type.