2011-09-25 61 views
9

在ARC環境中,我有以下代碼:類型轉換導致編譯器錯誤

NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; 
[invocation setTarget:delegate]; 
[invocation setSelector:@selector(restClient:loadedFile:contentType:eTag:)]; 
// Error Here! 
[invocation setArgument:&self atIndex:2]; 
[invocation setArgument:&filename atIndex:3]; 
[invocation setArgument:&contentType atIndex:4]; 
[invocation setArgument:&eTag atIndex:5]; 

參數設置爲索引2(&self)導致以下編譯器錯誤:

Sending *const __strong * to parameter of type void * changes retain/release properties

我不知道如何解決這個問題,同時保持有效的代碼。目前我只是堅持在NULL並將try語句包裝在try/catch塊中,但這是一個不太理想的解決方案。


類似的問題,如果有人是一種足以解決這一問題,以及:

有了這行代碼(從MPOAuth庫)

status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, (CFTypeRef *)&attributesDictionary); 

我收到以下錯誤

Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef ' (aka 'const void *') is disallowed with ARC

+1

是否有一些特定的原因,你爲什麼要在這裏使用NSInvocation而不是塊? – NSResponder

+0

我不確定,它是Dropbox SDK的一部分。我只是讓它符合ARC標準,儘量不要搞亂代碼。 – FeifanZ

回答

0

與其改變SDK(Dropbox表示他們將很快發佈ARC兼容版本),我發現我可以有選擇地將ARC用於文件。所以我做到了。

然後我升級到1.0b2,它被打包成一個庫,所以問題就解決了。

13

您應該能夠將其轉換爲適當的指針類型:

NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; 
[invocation setTarget:delegate]; 
[invocation setSelector:@selector(restClient:loadedFile:contentType:eTag:)]; 
Foo *foo = self; 
[invocation setArgument:&foo atIndex:2]; 
[invocation setArgument:&filename atIndex:3]; 
[invocation setArgument:&contentType atIndex:4]; 
[invocation setArgument:&eTag atIndex:5]; 
2

這一行:

status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, (CFTypeRef *)&attributesDictionary); 

可以解決如下:

CFTypeRef outDictionaryRef; 
status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &outDictionaryRef; 
attributesDictionary = (__bridge_transfer NSDictionary *) outDictionaryRef; 

因此,在本質只要給它預計爲出PARAM引用類型。當外部參數填寫完畢後,將所有權轉移到您的可可類型。