【问题标题】:Why do Apple headers not have __autoreleasing for NSError** params?为什么 Apple 标头没有 NSError** 参数的 __autoreleasing?
【发布时间】:2013-02-20 19:19:04
【问题描述】:

为什么 Apple 的采用 NSError** 的方法不声明为 NSError * __autoreleasing *

Transitioning to ARC Release Notes 似乎暗示它们应该是 (?)。

例如,NSFileManager.h 中的所有例程。但我实际上并没有看到 任何 Apple 标头使用了 Transitioning to ARC Release Notes 中的说明:

and the method declaration would typically be:

-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;

可能是因为支持遗留代码库,其中所有NSError * error = nil; 声明都将暗示strong,因此如果Apple 将__autoreleasing 放在那里,它会导致创建一个临时的__autoreleasing 局部变量每次?只有我能想到的。

【问题讨论】:

    标签: objective-c cocoa automatic-ref-counting out-parameters


    【解决方案1】:

    指向 Objective-C 对象的双指针隐式为 __autoreleasing,由 LLVM spec 指定。

    【讨论】:

    • 有趣的文档链接!这似乎与 Transitioning to ARC Release Notes 文档中所说的不符,它暗示将创建一个具有匹配存储类型的临时变量。 ??
    • @Dad 这些变量是编译器为 Pass By Writeback 创建的,它们不影响第二个指针的存储限定符。
    • 你引用的文档很清楚。我已经提交了一份关于“过渡到 ARC 发行说明”文档的文档说明请求,因为当与 Apple 标头相比时,它会让人感到困惑。感谢您的帮助!
    • 所以我想将局部变量声明为 __autoreleasing 会保存编译器(然后是一个局部变量寄存器).. NSError __autoreleasing * error = nil; [foo methodWithError: &error];因为否则 LLVM 规范和过渡到 ARC 发行说明表明它将创建该临时变量。对吗?
    • @Dad 这样做不值得。让编译器为您处理存储限定符。无论如何,这样优化是轻率的。
    猜你喜欢
    • 2014-06-09
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2021-01-05
    • 2011-08-01
    相关资源
    最近更新 更多