gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:16:29 45 4
gpt4 key购买 nike

在 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/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com