待研究
为什么
MJPopupViewController这个项目里使用了这个特性?有什么好处呢?
首先解答以下,为什么不能使用@synthesize 直接在Category里生成get 和 set 方法:
语法是不允许的!
如果我们在类里使用了
@property (nonatomic, retain) UIViewController *mj_popupViewController;
@property (nonatomic, retain) MJPopupBackgroundView *mj_popupBackgroundView;
那么,编译器自动会为我们生成对应的变量。但是,如果我们在category里这样使用:
@interface UIViewController (MJPopupViewController) @property (nonatomic, retain) UIViewController *mj_popupViewController; @property (nonatomic, retain) MJPopupBackgroundView *mj_popupBackgroundView; - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType; - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed; - (void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType; @end
编译器是不会生成变量的!
category 是加入新的方法用的
category与associative作为objective-c的扩展机制的两个特性,category即类型,可以通过它来扩展方法;associative,可以通过它来扩展属性;在iOS开发中,可能category比较常见,相对的associative,就用的比较少,要用它必须使用<objc/runtime.h>的头文件,然后就可以自由使用objc_getAssociatedObject以及objc_setAssociatedObject,我们来看下这两个方法:
原文:http://www.cnblogs.com/breezemist/p/5189360.html