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) 会导致以下编译器错误:

将 *const __strong * 发送到 void * 类型的参数会更改保留/释放属性

我不知道如何在保持有效代码的同时解决这个问题。目前我只是坚持NULL并将调用语句包装在 try/catch 块中,但这是一个不太理想的解决方案。


类似的问题,如果有人也愿意解决它:

使用这行代码(来自 MPOAuth 库)

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

我收到以下错误

ARC 不允许使用指向“CFTypeRef ”(又名“const void * ”)的 Objective-C 指针的间接指针强制转换

4

3 回答 3

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];
于 2011-09-27T05:24:09.480 回答
2

这一行:

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

可以解决如下:

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

所以本质上只是给出它期望的引用类型作为输出参数。当 out 参数填写完毕后,将所有权转移到您的可可类型。

于 2011-12-09T14:11:39.697 回答
0

与其更改 SDK(Dropbox 表示他们将很快发布与 ARC 兼容的版本),我发现我可以有选择地使用 ARC 来处理文件。所以我就这么做了。

然后我升级到 1.0b2,它被打包为一个库,这样问题就解决了。

于 2011-09-29T15:24:31.270 回答