Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top