NSInvocation and ARC (Automatic Reference Counting)

我们两清 提交于 2019-12-20 20:19:34

问题


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?


回答1:


This might work;

__unsafe_unretained NSString *one = @"Hello World";



回答2:


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.



来源:https://stackoverflow.com/questions/8811498/nsinvocation-and-arc-automatic-reference-counting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!