想突破自身的技术瓶颈,提高自身iOS开发水平,深挖 Runtime源码 一定是个不错的方法。本篇博客我们结合 Runtime源码讲一下 Objective-C Associated Objects 的实现原理。 由于在iOS开发中经常需要使用分类,如果我们需要为已经存在的类添加属性,使用 @property 并不能在分类中正确创建实例变量和存取方法,不过 通过 Objective-C 运行时中的关联对象,也就是 Associated Objects ,可以实现上述需求。

在这里我还是要推荐下我自己建的iOS开发学习群:680565220,群里都是学ios开发的,如果你正在学习ios ,小编欢迎你加入,今天分享的这个案例已经上传到群文件,大家都是软件开发党,不定期分享干货(只有iOS软件开发相关的),包括我自己整理的一份2018最新的iOS进阶资料和高级开发教程


读取上面的警告,我们得知, Category Property 属性的存取方法需要自己手动去实现,或者使用 @dynamic 在运行时实现这些方法。
换句话说,分类中 LLVM 和 @property 闹掰了, 并没有为我们生成实例变量以及存取方法,而需要我们手动实现。
单纯的技术辕们总是听不进去别人的话,总喜欢用代码来验证一些问题














网友评论