> For the complete documentation index, see [llms.txt](https://awesome-tips.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://awesome-tips.gitbook.io/ios/yu-yan/objective-c/content-10.md).

# 11. 弱持有容器方案

**作者**: [高老师很忙](https://weibo.com/517082456)

在通知者模式中，`manger` 想弱持有一些对象，我们可以怎么做呢？

方案一：使用`Foundation`为大家提供的弱持有容器：`NSHashTable`、`NSMapTable`、`NSPointerArray`，初始化时把`option`设置成`weak`即可；

方案二：可以使用`CFFoundation`的`CFArrayCreateMutable`等来创建容器，被添加的对象引用计数不会加1；

方案三：可以妙用`NSValue`的`valueWithNonretainedObject`方法，被添加的对象不需要服从`NSCopying`协议；

方案四：使用我们通常用的强持有容器，让被添加的对象weak一下，比如使用`__weak`和`block`配合：在`block`中返回 `weak` 对象。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://awesome-tips.gitbook.io/ios/yu-yan/objective-c/content-10.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
