In which situations do we need to write the __autoreleasing ownership qualifier under ARC?

后端 未结 3 654
醉梦人生
醉梦人生 2020-11-27 09:48

I\'m trying to complete the puzzle.

__strong is the default for all Objective-C retainable object pointers like NSObject, NSString, etc.. It\'s a strong

3条回答
  •  有刺的猬
    2020-11-27 10:28

    The definitive ARC specification says that

    For __autoreleasing objects, the new pointer is retained, autoreleased, and stored into the lvalue using primitive semantics.

    So for example, the code

    NSError* __autoreleasing error = someError;
    

    actually gets converted to

    NSError* error = [[someError retain] autorelease];
    

    ... which is why it works when you have a parameter NSError* __autoreleasing * errorPointer, the called method will then assign the error to *errorPointer and the above semantics will kick in.

    You could use __autoreleasing in a different context to force an ARC object into the autorelease pool, but that's not terribly useful since ARC only seems to use the autorelease pool at method return and already handles that automatically.

提交回复
热议问题