表达太复杂,无法在合理的时间内解决

时间:2016-01-05 11:47:13

标签: swift

我收到此错误:

  

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

从这段代码:

// 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

为什么会这样?

2 个答案:

答案 0 :(得分: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"

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

答案 1 :(得分:0)

在我的情况下,将每个变量转换为String都有帮助。

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