gpt4 book ai didi

ios - 尝试将指针转换为 ARC 下的指针

转载 作者:行者123 更新时间:2023-11-29 11:28:32 26 4
gpt4 key购买 nike

我遇到这个编译器错误:

Implicit conversion of a non-Objective-C pointer type 'void * _Nullable' to 'NSObject *__strong *' is disallowed with ARC

当我尝试编译这个在 ARC 之前有效的旧代码时。实际代码在一个由数组驱动的循环中。此片段只是一个循环值(value)的代码,用于突出显示没有无关内容的错误。

- (void)myCode
{
NSDictionary *sDict; // I want the @{ @"key" : @"value" } to end up here

// this is in a loop to set several dictionaries
// vcValue is set from a controlling array, but ends up like this:
NSValue *vcValue = [NSValue valueWithPointer:&sDict];
NSObject **vcPointer = [vcValue pointerValue]; // <<-- generates the error
*vcPointer = @{ @"key" : @"value" }; // for sDict
}

我正在使用 NSObject,因为根据控制数组,指针可能指向 NSDictionary 或 NSArray。在这个例子中,它指向一个字典。

定义 vcPointer 的正确方法是什么?

最佳答案

如果您不知道它是 NSDictionary * 还是 NSArray *,正确的类型是 id

NSDictionary *sDict;
NSValue *vcValue = [NSValue valueWithPointer:&sDict];
id __strong *vcPointer = (id __strong *)vcValue.pointerValue;
*vcPointer = @{ @"key": @"value" };

关于ios - 尝试将指针转换为 ARC 下的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123794/

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