instance variables may not be placed in categories
2015-12-25 15:34
666 查看
Avoid Properties in Categories
Objective-C分类中是不允许增加成员变量的(Instance variables may not be placed in categories),我们可以通过运行时函数objc_setAssociatedObject和
objc_getAssociatedObject来让分类支持保存和获取一些数据,从而支持属性。
//EOCPerson+FriendShip.h @interface EOCPerson (FriendShip) @property (nonatomic, strong) NSArray *friends; @end //EOCPerson+FriendShip.m static const char* kFriendsPropertyKey = "kFriendsPropertyKey"; @implementation EOCPerson (Friendship) - (NSArray*)friends { return objc_getAssociatedObject(self, kFriendsPropertyKey); } - (void)setFriends:(NSArray*)friends { objc_setAssociatedObject(self, kFriendsPropertyKey, friends, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end
相关文章推荐
- Can I change the order of items in category views?
- How to move items to another category
- How to use an FC category as Homepage / Frontpage
- Google分布式系统三驾马车: GFS,mapreduce,Bigtable
- mangos源码分析
- UVa 802 - Lead or Gold
- Go lang学习
- RockMongo安装使用笔记
- UFT12支持Goole Chrome的扩展程序
- mongo_1 新手之路
- Google 不推荐在 URL 里使用竖线
- django分页工具
- 从谷歌下载APK到电脑
- mongo常用手册
- Django1.8开发当中的异常处理
- django 模板里面for循环常用的方法
- 关于使用google的data binding library的一些常见错误
- cgi stress test, mongoose dead
- Go 基本命令
- osgOcean+VS2010+Win7编译方法