【问题标题】:Expression was too complex to be solved in reasonable time表达式过于复杂,无法在合理时间内解决
【发布时间】:2016-01-05 11:47:13
【问题描述】:

我收到此错误:

“表达式太复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式”

从此代码:

// Create a string of text that is used by search capabilites
var searchText = (capital.text + " " + nameEnglish.text + " " + nameLocal.text + " " + currencyCode.text).lowercaseString
updateObject["searchText"] = searchText

为什么会这样?

【问题讨论】:

  • 您的问题是什么?编译器会告诉你该怎么做。

标签: swift


【解决方案1】:

我接受了你的问题并创建了自己的测试用例并提出了这个:

// mock objects
let capital = UILabel()
capital.text = "A"
let nameEnglish = UILabel()
nameEnglish.text = "B"
let nameLocal = UILabel()
nameLocal.text = "C"
let currencyCode = UILabel()
currencyCode.text = "D"

var searchText = "\(capital.text) \(nameEnglish.text) \(nameLocal.text) \(currencyCode.text)".lowercaseString
print(searchText)

结果:

"可选("a") 可选("b") 可选("c") 可选("d")"

显然,添加 if-let 会导致:

if let cap = capital.text, neng = nameEnglish.text, nloc = nameLocal.text, curr = currencyCode.text {
    searchText = "\(cap) \(neng) \(nloc) \(curr)".lowercaseString
    print(searchText)
}

"a b c d"

编译器告诉你以你的方式连接字符串太复杂了。像这样或以增量方式连接它们,它会起作用。

【讨论】:

    【解决方案2】:

    将每个变量转换为字符串对我来说很有帮助。

    var searchText = (String(capital.text) + " " + String(nameEnglish.text) + " " + String(nameLocal.text) + " " + String(currencyCode.text)).lowercaseString
    

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多