据我所知,你不应该在类别中定义实例变量,如果你声明一个属性,那只意味着你声明了“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
对待类别中声明的属性与对待子类中声明的属性相同,因此它会为您动态实现访问器方法。
关于ios - 为什么CALayer的类别可以声明一个属性而不需要实现并直接使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793970/