【问题标题】:converting [AnyObject] to [Int] in swift 3在 swift 3 中将 [AnyObject] 转换为 [Int]
【发布时间】:2017-04-07 16:20:09
【问题描述】:

我正在尝试创建一个数字数组并让用户将它们保存在他们的设备上。然而,当数据被保存(通过使用NSUserDefaults 方法)时,它被转换为[AnyObject] 数组。我需要将其转换为[Int] 的数组。这是我目前的处理方法:

class ProgressViewController: UIViewController {
  let kUserDefault = UserDefaults.standard
  var chartData = [Int]()
  override func viewWillAppear(_ animated: Bool) {
    let weighting = UserDefaults.standard.array(forKey: "chartData")
    footer1.text = "\((weighting))"
}
  @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)!)

            self.kUserDefault.set([self.chartData], forKey: "chartData") //as? [Int]
            self.kUserDefault.set([self.chartLegend], forKey: "chartLegend")
            self.kUserDefault.synchronize()
}
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(confirmAction)
self.present(alert, animated: true, completion: nil)
 }

我尝试过let weightData: Int = weighting as [Int],但它崩溃了,说致命错误 我试过print("\(weighting?[0] as! Int)"),但是说Could not cast value of type '__NSCFArray' (0x109f6ae88) to 'NSNumber' (0x108df0300).会崩溃

当我尝试时

let weighting = UserDefaults.standard.array(forKey: "chartData") as! [Int]
print("\((weighting[0]))")

我的应用程序崩溃说Could not cast value of type '__NSCFArray' (0x108190e88) to 'NSNumber' (0x107016300)

有没有办法将保存的数组从[AnyObject] 转换为[String][Int]

【问题讨论】:

    标签: arrays swift xcode int anyobject


    【解决方案1】:

    在一行

    self.kUserDefault.set([self.chartData], forKey: "chartData")
    

    您正在保存[[Int]] 类型的对象,因为chartData 已经是一个数组。

    去掉括号

    self.kUserDefault.set(self.chartData, forKey: "chartData")
    

    【讨论】:

    • 它仍然给我同样的问题。
    • 使用上面的语法,您必须展平数组以将值分配给文本字段footer1.text = "\(weighting[0])"。基本上,您必须决定该数组是应该是Int aka [Int] 的数组还是[Int] aka [[Int]] 的数组。
    • 在玩弄了我的代码之后,它实际上工作了!谢谢!
    猜你喜欢
    • 2016-11-17
    • 1970-01-01
    • 2014-10-16
    • 2017-03-19
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多