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. AOP概念

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...

  2. 如何让FPGA中的SPI与其他模块互动起来

    在上一篇文章<FPGA的SPI从机模块实现>中,已经实现了SPI的从机模块,如何通过SPI总线与FPGA内部其他模块进行通信,是本文的主要讨论内容. 一. 新建FPGA内部DAC控制模块 ...

  3. [转]openlayer+geoserver实现WFS操作

    From:http://liushaobo2005.blog.163.com/blog/static/253056702011541462372/ wfs是OGC的标准规范,主要用于提供对矢量地理数据 ...

  4. Scrapy URLError

    错误信息如下: 2015-12-03 16:05:08 [scrapy] INFO: Scrapy 1.0.3 started (bot: LabelCrawler) 2015-12-03 16:05 ...

  5. 消息内容定义XML

    <XML>     <Title>title</Title>     <ContentType>text/plain</ContentType&g ...

  6. ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别

    本文来自:http://www.cnblogs.com/zhouxiaxue/archive/2006/05/12/398266.html http://www.cnblogs.com/yaoxc/a ...

  7. Android OpenCV实现图片叠加,水印

    关于如何用纯OpenCV实现图片叠加的例子实在是太少,太多的是使用 C++,JNI实现的,如果要用C++的话,我们为啥不转行做C++ 下面的例子基于 Android JavaCV 实现了在im_bea ...

  8. 谈谈 css 的各种居中——读编写高质量代码有感

    css 的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align:center; ...

  9. meta 标签整理

    <!-- 声明文档 --> <meta charset='utf-8'> <!-- 指示IE以目前可用的最高模式显示内容 --> <meta http-equ ...

  10. sublime text 2 安装

    下载 http://pan.baidu.com/s/1eRemu0y http://jingyan.baidu.com/album/ff4116259b057c12e48237b8.html?pici ...