9

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?

2 Answers 2

10

This might work;

__unsafe_unretained NSString *one = @"Hello World";
2
  • 3
    This will work, but you have to be VERY careful with the memory handling on this. I would recommend not using NSInvocation anymore if you can avoid it... Jan 10, 2012 at 22:53
  • 1
    I am using OCMock, and it passes an NSInvocation object into its callback blocks. If you don't use __unsafe_unretained on all the parameters ARC gets very unhappy. So thanks for the tip, mysterious crashes gone!
    – phatmann
    May 22, 2014 at 20:39
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.