ChenYilong / ChenYilong/iOSInterviewQuestions
第8题,runtime实现weak属性的一点问题
Open
- Dominant language
- Swift
- Stars
- 9.5k
- Forks
- 2.8k
- PR merge metrics
- No merged PRs in 30d
Description
在第8题中,NSObject的类目`NSObject (CYLRunAtDealloc)`中的方法`cyl_runAtDealloc:`的属性关联个人觉得有点疏漏:
在这个方法中,初始化`CYLBlockExecutor`后的属性关联的key采用的是固定的key`runAtDeallocBlockKey`,这样在多个对象同时弱引用另一个对象的时候,导致只能最后一个对象拥有对这个对象的弱引用。所以,我的想法是,这个key可以用动态的值,比如`executor`的地址:
```
CYLBlockExecutor *executor = [[CYLBlockExecutor alloc] initWithBlock:block];
const void * key = &executor;
objc_setAssociatedObject(self,
key,
executor,
OBJC_ASSOCIATION_RETAIN);
```
这样可以避免这个问题。
以上就是我的一点想法,仅供作者参考。
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.