简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM