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

标签 ios xcode casting automatic-ref-counting

我遇到这个编译器错误:

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/

相关文章:

iphone - 只允许用户按 UIButton 一次

ios - 辞职 First Responder 仅在一个文本字段中不工作

java - 如何调试 Spark 中的 ClassCastException 错误?

指定类型时 javac "uses unchecked or unsafe operations"

json - com.google.gson.JsonObject 不能转换为 com.google.gson.JsonArray

ios - 我可以访问表示层中 CAShapeLayer 的路径属性吗?

javascript - 如何在 iphone 上禁用 url 和底栏

jquery - iPad 设备上的溢出 x 和 y 未显示

swift - 在 swift 中覆盖系统 UI 样式时如何更改默认状态栏颜色

ios - 迪尔德 : Symbol not found: Xcode 11 crash