gpt4 book ai didi

ios - 如何使用 NS_RETURNS_INNER_POINTER 标志

转载 作者:可可西里 更新时间:2023-11-01 04:43:48 28 4
gpt4 key购买 nike

我执行一项采用现代 Objective-C 的任务,我在 Xcode 中使用重构工具:编辑 > 重构 > 转换为现代 Objective-C 语法。

在项目中我有一个方法返回 const void * 类型。并且在重构工具自动添加 NS_RETURNS_INNER_POINTER 标志之后这个方法之后。我确实在 FoundationOlderNotes 中检查过这个标志但我不清楚。

  1. 是否有必要为任何返回非对象指针类型的方法或属性添加NS_RETURNS_INNER_POINTER
  2. 编译器对 ARC 中方法的指针返回到底做了什么?

最佳答案

NS_RETURNS_INNER_POINTER 是 Cocoa 特有的 objc_returns_inner_pointer 方法属性的等价物。文档是 here :

An Objective-C method returning a non-retainable pointer may be annotated with the objc_returns_inner_pointer attribute to indicate that it returns a handle to the internal data of an object, and that this reference will be invalidated if the object is destroyed. When such a message is sent to an object, the object’s lifetime will be extended until at least the earliest of:

  • the last use of the returned pointer, or any pointer derived from it, in the calling function or
  • the autorelease pool is restored to a previous state.

Rationale

Rationale: not all memory and resources are managed with reference counts; it is common for objects to manage private resources in their own, private way. Typically these resources are completely encapsulated within the object, but some classes offer their users direct access for efficiency. If ARC is not aware of methods that return such “interior” pointers, its optimizations can cause the owning object to be reclaimed too soon. This attribute informs ARC that it must tread lightly.

The extension rules are somewhat intentionally vague. The autorelease pool limit is there to permit a simple implementation to simply retain and autorelease the receiver. The other limit permits some amount of optimization. The phrase “derived from” is intended to encompass the results both of pointer transformations, such as casts and arithmetic, and of loading from such derived pointers; furthermore, it applies whether or not such derivations are applied directly in the calling code or by other utility code (for example, the C library routine strchr). However, the implementation never need account for uses after a return from the code which calls the method returning an interior pointer.

作为异常(exception),如果接收器是直接从带有 precise lifetime semantics__strong 对象加载的,则不需要扩展。 .

Rationale

Implicit autoreleases carry the risk of significantly inflating memory use, so it’s important to provide users a way of avoiding these autoreleases. Tying this to precise lifetime semantics is ideal, as for local variables this requires a very explicit annotation, which allows ARC to trust the user with good cheer.

回答您的具体问题:

  1. 不,没有必要为返回非对象指针的每个方法或属性都用NS_RETURNS_INNER_POINTER 进行注释。只有在返回“内部”或“内部”指针时才应该这样做。也就是说,如果对象本身被释放,该指针将变得无效。
  2. 编译器具体做什么没有具体说明。在某些实现中,它可能只是保留并自动释放返回内部指针的对象。

关于ios - 如何使用 NS_RETURNS_INNER_POINTER 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com