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

instance variables may not be placed in categories的更多相关文章

  1. swift的属性与变量- Stored Properties and Instance Variables

    是一个概念 Stored Properties and Instance Variables If you have experience with Objective-C, you may know ...

  2. Instance Variables in ruby

    Dogs have many shared characteristics, like the abilities to wag their tails and drink water from a ...

  3. Class and Instance Variables In Ruby

    https://github.com/unixc3t/mydoc/blob/master/blog/caiv.md

  4. Effective Objective-C [上]

    网上看到的 http://esoftmobile.com/2013/08/10/effective-objective-c/ 本文是针对<Effective Objective-C>一书的 ...

  5. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  6. python class metaclass instance

    >>> class CObj(object):... pass...>>> dir()['CObj', '__builtins__', '__doc__', '__ ...

  7. Class Methods & Variables

    When calling an instance method like withdraw_securely, the syntax generally looks something like th ...

  8. Blocks and Variables

    Blocks and Variables https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Blocks/A ...

  9. 30天代码day4 Class vs. Instance

    Class A blueprint defining the charactaristics and behaviors of an object of that class type. Class ...

随机推荐

  1. Javascript数组操作方法

    1.shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] ...

  2. Eclipse setting Java code style and codetemplate

    1.open the eclipse tool window First click the Window menu,then check the children's menu which name ...

  3. 栈ADT的链表实现

    /* 栈ADT链表实现的类型声明 */ struct Node; typedef struct Ndoe *PtrToNode; typedef PtrToNode Stack; struct Nod ...

  4. KMP快速模式匹配的java实现

    假期实在无聊赖啊.把这个算法实现了一下即算是打发时间也算练练手了. KMP算法的关键是用归纳法计算失败函数.网上很详细了.下面直接给出代码. /** * * @author Vincent * */ ...

  5. iOS 自我检測

    1.id 和 NSObject的差别? 2.UITableViewCell的复用原理? 3.UIView生命周期和UILayer的差别? 4.多线程NSOperation和Queue.GDC.Thre ...

  6. LINQ 基本子句之一 (select/where/group/into)

    特别喜欢同事看到我写了一句小排序的时候说,他当然喜欢Linq了,虽然我只是baidu之,不知其然也不知其所以然. 基本格式 var<变量> = from <项目> in < ...

  7. 《think in python》学习-7

    think in python 7 迭代 对一个变量可以进行多次赋值 是合法的. 例如以下: bruce = 5 print bruce bruce = 7 print bruce 因为有了多重赋值 ...

  8. T-sql编程

    T-Sql中的变量都是@符号开头的 以一个@符号开头,叫做“用户声明的变量” 以两个@@开头的叫做"全局变量","系统变量",是由系统来维护的.无需我们维护 - ...

  9. Oracle利用过程procedure块实现银行转账

    -- create table account( id ) not null, name ), ,2), constraint a_pk primary key(id) ); --历史表 select ...

  10. How to Install/Deinstall Oracle Workspace Manager (文档 ID 263428.1)

    In this Document   Goal   Solution   References APPLIES TO: Workspace Manager - Version 9.0.1.0 to 1 ...