xcode 如果条件因表达式过于复杂而失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26550775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:59:06  来源:igfitidea点击:

If condition failing with expression too complex

xcodeif-statementswiftcompilation

提问by steventnorris

I have a conditional statement that claims the 'Expression was too complex to be solved in reasonable time. If there are any more than around 5 contains statements in my conditional, it fails with that error. This does not seem like something that should be happening on compile, seeing as the statement isn't all that complex. Is this a bug that anyone else has run into? Is there a solution other than splitting up my conditions?

我有一个条件语句,声称“表达式太复杂,无法在合理的时间内解决”。如果我的条件中包含超过 5 个左右的语句,则它会因该错误而失败。这似乎不是编译时应该发生的事情,因为语句并不是那么复杂。这是其他人遇到的错误吗?除了拆分我的条件之外,还有其他解决方案吗?

else if(
                contains(JSONDict.keys.array, "id") &&
                contains(JSONDict.keys.array, "part_number") &&
                contains(JSONDict.keys.array, "sales_part_number") &&
                contains(JSONDict.keys.array, "include_in_search") &&
                contains(JSONDict.keys.array, "description") &&
                contains(JSONDict.keys.array, "brand") &&
                contains(JSONDict.keys.array, "product_group") &&
                contains(JSONDict.keys.array, "product_design") &&
                contains(JSONDict.keys.array, "material") &&
                contains(JSONDict.keys.array, "line") &&
                contains(JSONDict.keys.array, "unit_of_mass") &&
                contains(JSONDict.keys.array, "coating") &&
                contains(JSONDict.keys.array, "pcs_converstion") &&
                contains(JSONDict.keys.array, "appRim") &&
                contains(JSONDict.keys.array, "appSegment") &&
                contains(JSONDict.keys.array, "series") &&
                contains(JSONDict.keys.array, "product_application")
                ){

            }

回答by Antonio

Yes that's a known issue - see also this answer.

是的,这是一个已知问题 - 另请参阅此答案

The solution is to store the logical expression into a variable, using a multiline statement:

解决方案是将逻辑表达式存储到变量中,使用多行语句:

else {
    var logicalExpression = contains(JSONDict.keys.array, "id") &&
            contains(JSONDict.keys.array, "part_number") &&
            contains(JSONDict.keys.array, "sales_part_number") &&
            contains(JSONDict.keys.array, "include_in_search")
    logicalExpression = logicalExpression && contains(JSONDict.keys.array, "description") &&
            contains(JSONDict.keys.array, "brand") &&
            contains(JSONDict.keys.array, "product_group") &&
            contains(JSONDict.keys.array, "product_design")
    // ... etc.
    if logicalExpression {
    }
}

A little weird for such a powerful language... but it's a (hopefully temporary) trade off.

对于如此强大的语言来说有点奇怪……但这是一个(希望是暂时的)权衡。