2012-09-21 3 views
1

프로젝트를 ARC로 변환하려고했는데이 오류로 인해 막혔습니다.ARC [rewriter] NSInvocation의 setArgument가 __unsafe_unretained 이외의 소유권을 가진 개체와 함께 사용하는 것이 안전하지 않습니다.

& 객체, & 호출 및 & callerToRetain 나에게 "[라이터]있는 NSInvocation의 setArgument이 __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]]; 
}} 

이 저를 도와주세요의 오류를 보이고있다.

+0

결국이 문제가 발생 했습니까? – Toby

답변

5

NSInvocation 기본적으로 NSInvocation은 주어진 인수를 효율적으로 유지하거나 복사하지 않으므로 호출 할 때 인수로 전달 된 각 객체는 계속 살아 있어야합니다. 즉, -setArgument : atIndex :로 전달 된 포인터는 __unsafe_unretained로 처리됩니다.

당신은 ARC에서 NSInvocation, 가장 간단한 방법을 사용하는 경우

는 작업을 진행

  1. 호출 객체를 생성 한 후 전화 [invocation retainArguments]입니다합니다. 즉, 호출은 주어진 인수를 유지합니다.
  2. 인수를 전달할 때 __unsafe_unretained으로 캐스팅하십시오.
  3. 3 단계가 없습니다.