Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a double pointer property in Objective-C?

I have a function declared like this:

- (void)loadWithCompletion:(MyCompletion)completion error:(NSError**)error;

The function takes a double pointer to an NSError so I can report errors to the caller. The completion (and possibly the error) will occur some time after the function is called. I need to store the NSError** as a property so I can use it when the aforementioned time passes.

@property(nonatomic, assign) NSError** error;

This property declaration gives me the error:

Pointer to non-const type NSError* with no explicit ownership.

like image 974
Pwner Avatar asked Feb 14 '23 06:02

Pwner


1 Answers

add __autoreleasing between the **, to give NSError*__autoreleasing* error

In Xcode 5.1 the ARC warning "Implicit ownership types on out parameters" was turned on by default (it used to be off). So with 5.1 this warning started appearing when there was no specified ownership.

The compiler assumes you want autoreleased, which is usually correct, but it's better that the author think about it and specify what the really want.

Usually you want the output parameter to be autoreleasing, similar to a function result. The caller will get an autoreleased object and will need to store it in a strong variable if they want to retain ownership.

like image 51
progrmr Avatar answered Mar 05 '23 20:03

progrmr