问题
@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