Objective-C: Instance Variable in Category

被刻印的时光 ゝ 提交于 2019-11-27 13:17:03
albertamg

A category can not declare additional instance variables but since OS X 10.6 and iOS 3.1 you can work around this with associative references.

You can use associative references to simulate the addition of object instance variables to an existing class. Using associative references, you can add storage to an object without modifying the class declaration. This is done via objc_setAssociatedObject and objc_getAssociatedObject. If you wrap these calls in a property’s custom getter and setter, you can fake an ivar.

Have a look at this post by Ole Begemann.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!