有時候?qū)慍ategory需要@property定義屬性嫌蚤。這個時候就需要使用@dynamic來進(jìn)行修飾。
@dynamic是什么意思?
@dynamic修飾就是表明這個屬性需要程序員自己管理該屬性的setter和getter方法
這個時候就需要重寫getter和setter方法了。
(簡單粗暴)直接上代碼
@interface UITableView (Extension) <TableViewDelegate>
@property (nonatomic, copy) NSString *name;
@end
#import "UITableView+Extension.h"
// 定義一個指針
static const void *Name = &Name;
@implementation UITableView (Extension)
@dynamic name;
- (void)setName:(NSString *)name {
objc_setAssociatedObject(self, Name, name, OBJC_ASSOCIATION_COPY);
}
- (NSString *)name {
return objc_getAssociatedObject(self, Name);
}