Properties on categories - how do I handle them in class instances?

牧云@^-^@ 提交于 2020-01-06 15:25:09

问题


@interface UIViewController (WithAdditionalProperties)
   @property (nonatomic, copy) void (^someBlock)();
   @property (strong, atomic) UIImageView *someView
@end

It would seem these properties aren't synthesized on UIViewController instances. When I access them in code, I get a NSInvalidArgumentException. But the compiler doesn't complain. What am I doing wrong?


回答1:


When you declare a property in a category you do not get the backing variable that you would normally get. You are merely adding a setter/getter to the class.

There is no built in way to make this work the way you are expecting but there is an alternative that leverages associated objects. This tutorial covers the topic better than anything I've seen on StackOverflow.

http://nshipster.com/associated-objects/



来源:https://stackoverflow.com/questions/24337633/properties-on-categories-how-do-i-handle-them-in-class-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!