我節省使用UserDefault
一個數組,在大多數情況下它的工作令人難以置信的偉大。然而,在一些仿真器,如iPhone 7,iPhone 5(的iOS 9.3),和iPhone 6(的iOS 9.3)在線路模擬器崩潰其中I嘗試加載我的陣列中的一個從UserDefaults。不能保存在某些iPhone模擬器UserDefault陣列(SWIFT 3)
@IBAction func AddGraphComponent(_ sender: UIButton) {
var weighText:UITextField!
let alert = UIAlertController(title: "Enter new Weight!", message: "Please enter your Weight, followed by the date in which your weight was recorded", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField { (weighText) in
weighText.placeholder = "Weight"
weighText.keyboardType = UIKeyboardType.numberPad
}
alert.addTextField { (dateText) in
dateText.placeholder = "Date (MM/YY)"
}
let confirmAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.default) { (_) in
let field = alert.textFields![0] as? UITextField
let weigh = alert.textFields![1] as? UITextField
if (field?.text)! == "" || (weigh?.text)! == "" {
let alert1 = UIAlertController(title: "Error!", message: "Please fill in BOTH fields", preferredStyle: UIAlertControllerStyle.alert)
alert1.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert1, animated: true, completion: nil)
}else {
print((field?.text)!,(weigh?.text)!)
let dataInt:Int = Int((field?.text!)!)!
self.chartData.append(dataInt)
self.chartLegend.append((weigh?.text)!)
var weighting = UserDefaults.standard.array(forKey: "chartData") as! [Int]
var dating = UserDefaults.standard.array(forKey: "chartLegend") as! [String]// this is where the error is EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP,subcode=0x0)
weighting.append((dataInt))
dating.append((weigh?.text)!)
self.kUserDefault.set(weighting, forKey: "chartData")
self.kUserDefault.set(dating, forKey: "chartLegend")
self.kUserDefault.synchronize()
self.lineChart.reloadData()
print("\((dating))")
print("\((weighting))")
}
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(confirmAction)
self.present(alert, animated: true, completion: nil)
}
在那裏我寫var dating = UserDefaults.standard.array(forKey: "chartLegend") as! [String]
我的代碼崩潰行表示設備模擬器。我想知道這是爲什麼。在我的代碼中是否有錯誤,或者這是一個錯誤? 我上的Xcode 8.0使用SWIFT 3
僅供參考 - 你看到這裏的問題是,爲什麼你要始終堅持從任何設備/軟件你一直在與發展中國家刪除您的應用程序一個最好的例子,然後做一個乾淨的構建和全新安裝,然後徹底測試在將您的應用程序發送給Apple之前,您的整個應用程序都已啓動隨着時間的推移,在開發設備上工作的事情並不少見,但在新安裝時會發生崩潰。 – rmaddy
@rmaddy謝謝你的建議!我一定會保留你所說的內容:) – CoolMAn