gpt4 book ai didi

objective-c - iOS 的 objective-c/cocoa 中存在哪些方法系列?

转载 作者:可可西里 更新时间:2023-11-01 06:18:35 27 4
gpt4 key购买 nike

我是一位经验丰富的开发人员,是 iOS 开发的新手(尤其是在 iPhone 上)。我正在从事的一个项目正在使用自动引用计数 (ARC)。

我在研究 ARC 时发现了这个文档:http://clang.llvm.org/docs/AutomaticReferenceCounting.html在标题为“保留的返回值”的部分中,它指出可以使用一个属性来指示调用者希望获得 +1 保留计数的所有权。它还继续指出,如果 ARC 检测到它属于特定方法系列(它具体命名为:alloc、copy、init、mutableCopy 和 new),它将自动将属性添加到方法中

从进一步的阅读来看,方法族似乎只是方法命名约定。我的理解是,如果方法名称以方法族开头,那么它就在该方法族中。例如

+(id) init
+(id) initWithName:(NSString*)name

都是 init 方法家族的一部分。

我的问题是:是否有用于 iOS 开发的已定义方法系列的正式列表?如果有,它们是什么/我可以在哪里找到它?

下面是我上面提到的 llvm.org 部分:

Section 3.2.2: Retained return values states:

A function or method which returns a retainable object pointer type may be marked as returning a retained value, signifying that the caller expects to take ownership of a +1 retain count. This is done by adding the ns_returns_retained attribute to the function or method declaration, like so:

id foo(void) __attribute((ns_returns_retained)); - (id) foo __attribute((ns_returns_retained)); This attribute is part of the type of the function or method.

When returning from such a function or method, ARC retains the value at the point of evaluation of the return statement, before leaving all local scopes.

When receiving a return result from such a function or method, ARC releases the value at the end of the full-expression it is contained within, subject to the usual optimizations for local values.

Rationale: this formalizes direct transfers of ownership from a callee to a caller. The most common scenario this models is the retained return from init, alloc, new, and copy methods, but there are other cases in the frameworks. After optimization there are typically no extra retains and releases required.

Methods in the alloc, copy, init, mutableCopy, and new families are implicitly marked attribute((ns_returns_retained)). This may be suppressed by explicitly marking the method attribute((ns_returns_not_retained)).

It is undefined behavior if the method to which an Objective-C message send statically resolves has different retain semantics on its result from the method it dynamically resolves to. It is undefined behavior if a block or function call is made through a static type with different retain semantics on its result from the implementation of the called block or function.

Rationale: Mismatches with returned results will cause over-retains or over-releases, depending on the direction. Again, the rule about function calls is really just an application of the existing C/C++ rule about calling functions through an incompatible function type.

最佳答案

Section 5 of the same document标题为“方法族”。它列出了所有当前定义的系列,也就是您在上面确定的系列; allocnewcopymutableCopyinit。它还列出了成为该家庭成员的标准。大多数情况下,它仅基于方法名称,但也取决于返回类型。例如,copy 家族的成员必须返回一个对象;具有 void 返回类型的复制方法将不会被视为 copy 方法系列的一部分。

关于objective-c - iOS 的 objective-c/cocoa 中存在哪些方法系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8771131/

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