为什么CALayer的category可以声明一个属性不实现直接使用?

why can CALayer's category declare a property without implementation and use it directly?

据我所知,你不应该在类别中定义实例变量,如果你声明了一个属性,那只意味着你声明了"setter & getter"方法。

@interface CALayer (XQ)
    @property NSString *demoVar;
    - (void)demoFunc;
@end
@implementation CALayer (XQ)
    - (void)demoFunc {
        self.demoVar = @"cuteeeee";
        NSLog(@"%@", self.demoVar);// when i call this method, it should crash, but the output is normal, why?
    }
@end

对不起,我的英语和短语不好,谢谢。

CALayer.h 中的评论指出:

/** Property methods. **/

/* CALayer implements the standard NSKeyValueCoding protocol for all
 * Objective C properties defined by the class and its subclasses. It
 * dynamically implements missing accessor methods for properties
 * declared by subclasses.

显然 CALayer 对待类别中声明的属性与对待子类中声明的属性相同,因此它会为您动态实现访问器方法。