【问题标题】:Type conversion of &self causes compiler error&self的类型转换导致编译器错误
【发布时间】:2011-09-25 23:03:21
【问题描述】:

在 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指针的间接指针强制转换

【问题讨论】:

  • 有什么特别的原因为什么要在这里使用 NSInvocation 而不是块?
  • 我不确定,它是 Dropbox SDK 的一部分。我只是让它符合 ARC,尽量不要把代码弄乱太多。

标签: objective-c ios xcode4 automatic-ref-counting


【解决方案1】:

您应该能够对其进行强制转换以获得适当的指针类型:

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;
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-20
        • 2013-05-14
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多