斯威夫特“表达太复杂,无法在合理的时间内解决”

时间:2017-03-28 02:33:48

标签: swift

var KEB_Hana: [String: Any] = [:]

override func viewDidLoad() {
    super.viewDidLoad()
    perform(#selector(LaunchingViewController.showLaunch), with: nil, afterDelay: 2)
    // Do any additional setup after loading the view.
    indi.startAnimating()
    //        if let value = UserDefaults.standard.string(forKey: "introMessage") {
    //            introMessage.text = UserDefaults.standard.string(forKey: "introMessage")
    //        }

    if let value = UserDefaults.standard.object(forKey: "userBeacon") {
        KEB_Hana = UserDefaults.standard.object(forKey: "userBeacon") as! [String : Any]
        print(KEB_Hana)

        var Edit_Hana = "Beacon0 : " + KEB_Hana["beacon0"] + "Beacon1 : " + KEB_Hana["beacon1"] + "Beacon2 : " + KEB_Hana["beacon2"] + "Beacon3 : " + KEB_Hana["beacon3"]

        introMessage.text = Edit_Hana
    }
}

为什么顶级代码不起作用? 它代表“表达太复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式”

1 个答案:

答案 0 :(得分:0)

编译器放弃了尝试猜测Edit_Hana的类型。假设这些键的值是字符串:

let Edit_Hana = ["beacon0", "beacon1", "beacon2", "beacon3"]
    .map { "\($0) : \(KEB_Hana[$0] ?? "n/a")" }
    .joined(separator: ". ")

如果你想粗心大意,你可以使用字符串插值:

let Edit_Hana = "Beacon0 : \(KEB_Hana["beacon0"]!). Beacon1 : \(KEB_Hana["beacon1"]!). Beacon2 : \(KEB_Hana["beacon2"]!). Beacon3 : \(KEB_Hana["beacon3"]!)"