Expected method to read dictionary element not found on object of type 'id<NSCopy>

214 Views Asked by At

I am trying to upgrade my app to Xcode 9.3.1 from 8 and have these errors:

Expected method to read dictionary element not found on object of type 'id<NSCopying>'

My code is:

// 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),
};

Can anyone point me in the right direction? I have bolded the errors in the code.

2

There are 2 best solutions below

0
Carl Lindberg On

The compiler thinks that options is of type id<NSCopying>, not an NSDictionary *, which is required to use the dictionary[key] syntax. Your code snippet does not include where that is declared, which is where the error would be.

0
K-AnGaMa On

You must cast your object options

    // 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),
};