objective-c - &self 的类型转换导致编译器错误

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

在 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 并将 invoke 语句包装在 try/catch block 中,但这是一个不太理想的解决方案。


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

使用这行代码(来自 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

最佳答案

你应该能够转换它以获得合适的指针类型:

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];

关于objective-c - &self 的类型转换导致编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549209/

相关文章:

c++ - Xcode 4(或 3)iOS,带 C++

ios - 在 UITableView 中搜索 RSS 源

ios - 我怎样才能解除分配我的 View Controller ?

ios - Push Segue 的奇怪错误

objective-c - 尝试制作 sqlite 的可写副本时出现错误

cocoa - 无法让简单的工具栏发挥作用!

objective-c - 与 scanf 函数一起使用

iphone - 了解 Soundcloud cocoa 包装器 api

ios - 关闭 UIImagePickerController

ios - 视频应用程序将 .mp4 保存到相机胶卷