2016-11-28 36 views
0

我節省使用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

+0

僅供參考 - 你看到這裏的問題是,爲什麼你要始終堅持從任何設備/軟件你一直在與發展中國家刪除您的應用程序一個最好的例子,然後做一個乾淨的構建和全新安裝,然後徹底測試在將您的應用程序發送給Apple之前,您的整個應用程序都已啓動隨着時間的推移,在開發設備上工作的事情並不少見,但在新安裝時會發生崩潰。 – rmaddy

+0

@rmaddy謝謝你的建議!我一定會保留你所說的內容:) – CoolMAn

回答

1

發生這種情況最有可能是因爲UserDefaults沒有找到名爲「chartData」或「chartLegend」什麼,然後閱讀它作爲零。你想要做的就是使用if let來安全地檢查是否有可以接收的值。

到這裏看看:

if let weighting = UserDefaults.standard.array(forKey: "chartData") as? [Int] { 
    //Do your stuff 
    weighting.append((dataInt)) 
    self.kUserDefault.set(weighting, forKey: "chartData") 
    print("\((weighting))") 
} 
if let dating = UserDefaults.standard.array(forKey: "chartLegend") as? [String] { 
    //do your stuff here 
    dating.append((weigh?.text)!) 
    self.kUserDefault.set(dating, forKey: "chartLegend") 
    print("\((dating))") 
} 
self.kUserDefault.synchronize() 

由於rmaddy評論,你做對模擬器和真實設備一個乾淨的安裝是非常重要的測試修改後的代碼之前,尤其是當你面對的讀/從存儲器寫。

編輯:

另一種方法是使用保護聲明。

guard let dating = UserDefaults.standard.array(forKey: "chartLegend") as? [String] 
else{ 
//dating is nil 
} 
+0

謝謝!這是非常有用的:)是否有任何方法來檢查否則(如果它確實是一個零)?我嘗試過使用'else {'方法,但它似乎並不工作 – CoolMAn

+0

我相信應該工作 – Starlord

+0

使用'guard let'方法而不是簡單的'if let'方法的好處是什麼? – CoolMAn

1

您檢索您設置前陣,這意味着有很好的機會,這是nil,你是力as!鑄造它,這是別的東西,你應該避免,因爲在這種情況下,它會崩潰。

我建議你做var dating = UserDefaults.standard.array(forKey: "chartLegend") as? [String] ?? [String](),而不是防止崩潰,並獲得更多的熟悉一般選配。

我的規則是永遠不要使用!解開的IBOutlets可選之外。我用ifguard??代替。