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. U盘开发之GPIF Master模式

    在U盘开发过程中,一个人从头做到尾,经常遇到一些莫名其妙的问题,只有到官网论坛发帖.折腾困扰我最久的,就是U盘的读写性能问题了,不采用GPIF的方式,single读只有几百K,single写只有几十K ...

  2. 【BZOJ 1088 扫雷Mine】模拟

    http://www.lydsy.com/JudgeOnline/problem.php?id=1088 2*N的扫雷棋盘,第二列的值a[i]记录第 i 个格子和它8连通的格子里面雷的数目. 第一列的 ...

  3. RPATH与RUNPATH

    RPATH与RUNPATH 时间 2011-11-01 21:46:44 Qt Labs China 原文  http://labs.qt.nokia.com.cn/2011/11/01/rpath- ...

  4. Xcoder 7.0 免证书真机测试

    相信大家已经看了WWDC大会上的内容了,在iOS9和Xcoder7.0以后真机测试不需要在购买付费账号了,(当然你要想上传appstore还是需要付费账号的). 今天我带大家来看下免证书的真机测试如何 ...

  5. JavaScript 自动生成 年月范围 选择

    近日做项目涉及到日期选择,为了用户界面友好,于是加入了一年内的年月段的查询功能,先看效果 会自动判断当前年份 以下为html代码 其中用到了 Jquery 和 struts 标签 但是这两个都不是重要 ...

  6. SQL_SERVER日期函数详细用法

    1.一个月第一天的 Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 2.本周的星期一 Select DATEADD(wk, DATEDIFF(wk,0, ...

  7. 代码混淆 GSON完满解决

    头疼的问题,json使用了google的gson三方包,可是混淆的时候出了问题 明明已经按照gson的官方文档,把混淆脚本加上去了,却还是出问题. 今天同事找到一篇博客,关于这个问题的: 我们是将js ...

  8. Android Studio无法关联Api23源码-提示Souces for android api 23 platform not found

    最近升级了As,然后忽然就关联不上源码了,很不方便,找个Activity的源码都只有outline没有解释,还提示这个错误: Decompiled .class file, bytecode vers ...

  9. I/O重定向与管道

    1.输出重定向 (1)>  覆盖输出 (2)>>  追加输出 (3) set -C: 禁止对已经存在文件使用覆盖重定向: 强制覆盖输出,则使用 >| set +C: 关闭上述功 ...

  10. [string]Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...