我正在将一段代码迁移到自动引用计数(ARC),并让ARC迁移器抛出错误
NSInvocation的的setArgument是不是安全,可以使用一个对象,具有比__unsafe_unretained其他所有制
在我使用类似的东西分配对象的代码上
NSDecimalNumber *testNumber1 = [[NSDecimalNumber alloc] initWithString:@"1.0"];
Run Code Online (Sandbox Code Playgroud)
然后使用将其设置为NSInvocation参数
[theInvocation setArgument:&testNumber1 atIndex:2];
Run Code Online (Sandbox Code Playgroud)
为什么它阻止你这样做?使用__unsafe_unretained对象作为参数似乎同样糟糕.例如,以下代码导致ARC下崩溃:
NSDecimalNumber *testNumber1 = [[NSDecimalNumber alloc] initWithString:@"1.0"];
NSMutableArray *testArray = [[NSMutableArray alloc] init];
__unsafe_unretained NSDecimalNumber *tempNumber = testNumber1;
NSLog(@"Array count before invocation: %ld", [testArray count]);
// [testArray addObject:testNumber1];
SEL theSelector = @selector(addObject:);
NSMethodSignature *sig = [testArray methodSignatureForSelector:theSelector];
NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:sig];
[theInvocation setTarget:testArray];
[theInvocation setSelector:theSelector];
[theInvocation setArgument:&tempNumber atIndex:2];
// [theInvocation retainArguments];
// Let's say we don't use this invocation until after the original pointer …Run Code Online (Sandbox Code Playgroud)