0

我正在尝试将我的应用程序从 8 升级到 Xcode 9.3.1 并出现以下错误:

在“id<NSCopying>”类型的对象上找不到读取字典元素的预期方法

我的代码是:

// Normalize the blending mode to use for the key.
// *** Error on next three lines ***
id src = (options[CCBlendFuncSrcColor] ?: @(GL_ONE));
id dst = (options[CCBlendFuncDstColor] ?: @(GL_ZERO));
id equation = (options[CCBlendEquationColor] ?: @(GL_FUNC_ADD));

NSDictionary *normalized = @{
    CCBlendFuncSrcColor: src,
    CCBlendFuncDstColor: dst,
    CCBlendEquationColor: equation,

    // Assume they meant non-separate blending if they didn't fill in the keys.
    // *** Error on next line ***
    CCBlendFuncSrcAlpha: (options[CCBlendFuncSrcAlpha] ?: src),
    CCBlendFuncDstAlpha: (options[CCBlendFuncDstAlpha] ?: dst),
    CCBlendEquationAlpha: (options[CCBlendEquationAlpha] ?: equation),
};

谁能指出我正确的方向?我已将代码中的错误加粗。

4

2 回答 2

1

编译器认为它options是 type id<NSCopying>,而不是 NSDictionary *,这是使用 dictionary[key] 语法所必需的。您的代码片段不包括声明的位置,即错误所在的位置。

于 2018-05-12T05:15:33.597 回答
0

您必须投射您的对象选项

    // Normalize the blending mode to use for the key.
id src = (((NSDictionary *)options)[CCBlendFuncSrcColor] ?: @(GL_ONE));
id dst = (((NSDictionary *)options)[CCBlendFuncDstColor] ?: @(GL_ZERO));
id equation = (((NSDictionary *)options)[CCBlendEquationColor] ?: @(GL_FUNC_ADD));

NSDictionary *normalized = @{
    CCBlendFuncSrcColor: src,
    CCBlendFuncDstColor: dst,
    CCBlendEquationColor: equation,

    // Assume they meant non-separate blending if they didn't fill in the keys.
    CCBlendFuncSrcAlpha: (((NSDictionary *)options)[CCBlendFuncSrcAlpha] ?: src),
    CCBlendFuncDstAlpha: (((NSDictionary *)options)[CCBlendFuncDstAlpha] ?: dst),
    CCBlendEquationAlpha: (((NSDictionary *)options)[CCBlendEquationAlpha] ?: equation),
};
于 2018-05-22T20:45:30.183 回答