一.開發(fā)技巧
1.在UIView的category中添加一個(gè)自定義的的字段,例如:
@interfaceUIView (UIView)
@property(nonatomic, strong) NSString *code;
@end
@implementationUIView (UIView)
- (void)setCode:(NSString*)code {
objc_setAssociatedObject(self,@selector(setCode:), code,OBJC_ASSOCIATION_RETAIN);
}
- (NSString*)code {
returnobjc_getAssociatedObject(self,@selector(setCode:));
}
@end
這樣一來就給view添加了一個(gè)code屬性摹蘑。介紹一個(gè)使用場景:
// _nameText.text 顯性的引用 userName 這個(gè)值
_nameText.text = userName
// _nameText.code 隱性的引用 userID 這個(gè)值
_nameText.code = userID
這個(gè)方法可以用于經(jīng)常使用的一對搭配的數(shù)據(jù)鉴逞,例如:userName & userID归斤、contractName & contractID等等枝冀。