1

プロジェクトを ARC に変換しようとしましたが、このエラーが発生しました。

&object,&invocation と &callerToRetain で、「[rewriter] NSInvocation's setArgument is not safe to be used with an object with Ownership without __unsafe_unretained」というエラーが表示される

+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)callerToRetain{if ([*target respondsToSelector:selector]) {
    NSMethodSignature *signature = nil;
    signature = [*target methodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:selector];

    int argumentNumber = 2;

    // If we got an object parameter, we pass a pointer to the object pointer
    if (object) {
        [invocation setArgument:&object atIndex:argumentNumber];
        argumentNumber++;
    }

    // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer to it
    if (amount) {
        [invocation setArgument:amount atIndex:argumentNumber];
    }

    SEL callback = @selector(performInvocation:onTarget:releasingObject:);
    NSMethodSignature *cbSignature = [ASIHTTPRequest methodSignatureForSelector:callback];
    NSInvocation *cbInvocation = [NSInvocation invocationWithMethodSignature:cbSignature];
    [cbInvocation setSelector:callback];
    [cbInvocation setTarget:self];
    [cbInvocation setArgument:&invocation atIndex:2];
    [cbInvocation setArgument:&target atIndex:3];
    if (callerToRetain) {
        [cbInvocation setArgument:&callerToRetain atIndex:4];
    }

    CFRetain(invocation);

    // Used to pass in a request that we must retain until after the call
    // We're using CFRetain rather than [callerToRetain retain] so things to avoid earthquakes when using garbage collection
    if (callerToRetain) {
        CFRetain(callerToRetain);
    }
    [cbInvocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:[NSThread isMainThread]];
}}

私を助けてください。

4

1 に答える 1

5

デフォルトでは、効率のNSInvocationために与えられた引数を保持またはコピーしないため、引数として渡された各オブジェクトは、呼び出しが呼び出されたときにまだ生きている必要があります。つまり、-setArgument:atIndex: に渡されるポインターは __unsafe_unretained として処理されます。

ARCで使用する場合NSInvocation、それを機能させる最も簡単な方法は

  1. 呼び出し[invocation retainArguments]オブジェクトを作成した後に呼び出します。つまり、呼び出しは指定された引数を保持します。
  2. 引数を渡すときは、それらを にキャストします__unsafe_unretained
  3. ステップ3はありません。
于 2013-04-23T14:00:23.893 に答える