Pretend I have a category on NSObject that defines the following method:
+ (instancetype)allocTemplate
{
id instance = [self new];
return instance;
}
and I have the following class:
@interface FDActor : NSObject
@property (nonatomic, copy) NSString *name;
+ (void)sayHi;
@end
@implementation FDActor
+ (void)sayHi
{
[self allocTemplate].name;
}
@end
How come [self allocTemplate].name
errors out at compile time if self is FDActor?
I am aware it works if you use normal message sending syntax but I am explicitly interested in the dot syntax error.
+ (instancetype)allocTemplate
on FDActor as well and got the same result. – Syncopateself
pointer usingself->_ivar
(the compiler does this for you if you use the ivar name only). Whether or not to use dot syntax for properties is a matter of code style/personal taste. The OP's question is really only related to how the compiler interprets methods returning aninstancetype
. – Soares