ios - 为什么CALayer的类别可以声明一个属性而不需要实现并直接使用它?

标签 ios calayer objective-c-category

据我所知,你不应该在类别中定义实例变量,如果你声明一个属性,那只意味着你声明了“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/

相关文章:

ios - 表情符号不显示在推送通知中

ios - 我想知道为什么 UIImagePickerController 裁剪正方形有轻微的偏移,以及如何修复它

iphone - 如何在动画完成时删除图层?

ios - 需要释放包含在 CALayer 的内容属性中的 CGImage

ios - 抑制链接器警告 : "Meta method X in category from Y overrides method from class in Z"

ios - 从嵌套数据数组向 UITableViewCell 中的 UITableView 填充数据

ios - 在 iOS 中使用 SOAP Web 服务

ios - CALayer压力变形效果

c++ - 使用类别方法在 NSDecimalNumber 上重载算术运算符

ios - 为什么我们需要在IOS中使用category?