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

后端 未结 3 753
醉梦人生
醉梦人生 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:26

    Following up on Macmade's answer and Proud Member's follow up question in the comments, (would have also posted this as a comment but it exceeds the max character count):

    Here is why the variable qualifier of __autoreleasing is placed between the two stars.

    To preface, the correct syntax for declaring an object pointer with a qualifier is:

    NSError * __qualifier someError;
    

    The compiler will forgive this:

    __qualifier NSError *someError;
    

    but it isn't correct. See the Apple ARC transition guide (read the section that begins "You should decorate variables correctly...").

    To address to the question at hand: A double pointer cannot have an ARC memory management qualifier because a pointer that points to a memory address is a pointer to a primitive type, not a pointer to an object. However, when you declare a double pointer, ARC does want to know what the memory management rules are for the second pointer. That's why double pointer variables are specified as:

    SomeClass * __qualifier *someVariable;
    

    So in the case of a method argument that is a double NSError pointer, the data type is declared as:

    - (BOOL)save:(NSError* __autoreleasing *)errorPointer;
    

    which in English says "pointer to an __autoreleasing NSError object pointer".

提交回复
热议问题