Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInvocation and ARC (Automatic Reference Counting)

When trying to migrate my current code to ARC, I'm getting errors whenever I pass an NSString as an NSInvocation argument.

Example:

NSInvocation inv = ...;
NSString *one = @"Hello World!";
[inv setArgument:&one atIndex:2];

The error happens when I use the Refactor -> Convert to Objective-C ARC option from the Edit menu. The text is "NSInvocation's setArgument is not safe to be used with an object with ownership other than __unsafe_retained."

How would I get around this?

like image 381
Anonymous Avatar asked Jan 10 '12 22:01

Anonymous


2 Answers

This might work;

__unsafe_unretained NSString *one = @"Hello World";
like image 138
Abizern Avatar answered Sep 25 '22 20:09

Abizern


As Joshua Weinberg commented, using NSInvocation is not recommended anymore. If you have up to two parameters you can use performSelector. For three parameters or more, you can use NSObject's -methodForSelector: as explained here.

like image 42
Yoav Avatar answered Sep 23 '22 20:09

Yoav