0
我有一個UIPickerView
可讓用戶更改國家設置。NSUserDefaults對象在執行委託方法後沒有進入
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(countries[row], forKey: "SelectedCountry")
let test = defaults.stringForKey("SelectedCountry")
delegate?.countryChanged()
}
這個test
變量得到正確的值。
然而,當委託方法countryChanged叫我得到一個錯誤:
fatal error: unexpectedly found nil while unwrapping an Optional value
這是代碼:
func countryChanged() {
let defaults = NSUserDefaults.standardUserDefaults()
countryLabel.text = defaults.stringForKey("SelectedCountry")
}
的價值怎麼能是零?這是調用委託方法之前的正確國家,並且沒有修改此值的代碼。
能否請您分享您的sameple項目? – Max
您應該確保'countries [row]'實際上包含一個'String'。 –
FUNC countryChanged(){ 讓默認= NSUserDefaults.standardUserDefaults() 如果讓文本= defaults.stringForKey( 「SELECTEDCOUNTRY」){ countryLabel?的.text =文本 } } – Lukas