为什么CALayer的类别可以在没有实现的情况下声明属性并直接使用它?

时间:2016-08-05 16:32:42

标签: ios calayer objective-c-category

据我所知,你不应该在一个类别中定义一个实例变量,如果你声明了一个属性,那只意味着你要对这个属性进行声明。吸气剂"方法

@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

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

1 个答案:

答案 0 :(得分:2)

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处理类别中声明的属性与处理子类中声明的属性相同,因此它会为您动态实现访问器方法。