ChenYilong / ChenYilong/iOSInterviewQuestions

第8题 感觉objc_storeWeak(&a, b)哪里理解有点问题

Open
#98 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
9.5k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

objc_storeWeak函数把第二个参数--赋值对象(b)的内存地址作为键值key,将第一个参数--weak修饰的属性变量(a)的内存地址(&a)作为value,注册到 weak 表中。如果第二个参数(b)为0(nil),那么把变量(a)的内存地址(&a)从weak表中删除,
你可以把objc_storeWeak(&a, b)理解为:objc_storeWeak(value, key),并且当key变nil,将value置nil。

```c

/**
* This function stores a new value into a __weak variable. It would
* be used anywhere a __weak variable is the target of an assignment.
*
* @param location The address of the weak pointer itself
* @param obj The new object this weak ptr should now point to
*
* @return The value stored into \e location, i.e. \e obj
*/
OBJC_EXPORT id _Nullable
objc_storeWeak(id _Nullable * _Nonnull location, id _Nullable obj)
OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
```

根据Apple的注视,应该是第一个&a 是作为key的 b是作为value的,当value变为nil后,将以&a为key的键值对从weak 表中移除

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.