iOS 中 为cagetory 添加属性

时间:2014-05-09 00:50:02   收藏:0   阅读:521

我们知道 在iOS中cagetory里面只能添加方法 ,如果我们想添加属性 就要用到

<objc/runtime.h>

OBJC_EXPORT void objc_setAssociatedObject(idobject, constvoid *key, id value, objc_AssociationPolicy policy)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
OBJC_EXPORT id objc_getAssociatedObject(idobject, constvoid *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);



例如

@interface UIButton(custom)

@property (nonatomic, copy) NSIndexPath *selectIndexPath;

@end


staticNSIndexPath *k_SelectIndexPath;


@implementation UIButton(custom)

@dynamic selectIndexPath;

- (void)setSelectIndexPath:(NSIndexPath *)selectIndexPath

{

objc_setAssociatedObject(self, &k_SelectIndexPath, selectIndexPath, OBJC_ASSOCIATION_COPY);

}


- (NSIndexPath*)selectIndexPath

{

returnobjc_getAssociatedObject(self, &k_SelectIndexPath);

}

@end





iOS 中 为cagetory 添加属性,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!