Swift:将UserDefaults字典数据检索到标签中

时间:2018-03-18 08:21:31

标签: ios swift dictionary nsuserdefaults

我是快速编程的新手。我将一些文本字段数据保存到字典中作为UserDefaults。我需要检索这些数据并以下列格式显示它。

x磅= x ound = x克

这是我在字典中将其保存到UserDefaults的代码。

 var weightDict = [String:Any]()
  var weightArray =  [Any]()

@IBAction func saveWeight(_ sender: Any) {


         weightDict = [:]
         weightDict.updateValue(textOunce.text, forKey: "ounce")
         weightDict.updateValue(textPound.text, forKey: "pound")
         weightDict.updateValue(textGram.text, forKey: "gram")
       weightArray.append(weightDict)


        let defaults = UserDefaults.standard

         defaults.set(weightArray,forKey: "savedWeightConversion")

        if let savedWeights = defaults.object(forKey: "savedWeightConversion"){
            weightArray = (savedWeights as! NSArray) as! [Any]
            print("value is",weightArray)
        }

    }

在应用程序加载时查看

override func viewDidAppear(_ animated: Bool) {
        let defaults = UserDefaults.standard
    let savedWeights = defaults.object(forKey: "savedWeightConversion") as? [String] ?? [String]()


    }

2 个答案:

答案 0 :(得分:1)

你可以尝试

  if let savedWeights = UserDefaults.standard.object(forKey: "savedWeightConversion") as? [Any] {

        if let dic  = savedWeights[0] as? [String:Any] {

            if let ounce  = dic["ounce"] as? String {

               self.ounceLb.text = ounce

            }

            if let pound  = dic["pound"] as? String {

                self.poundLb.text = pound

            }

            if let gram  = dic["gram"] as? String {

                self.gramLb.text = gram

            }

        }

    }

答案 1 :(得分:1)

试试这个

override func viewWillAppear(_ animated: Bool) {
    if let savedWeights = UserDefaults.standard.object(forKey: "savedWeightConversion") as? [Any] {
        for i in 0 ..< savedWeights.count{
            if let weightDict  = savedWeights[i] as? [String:Any] {
                print("\(weightDict["pound"] as! String) pounds= \(weightDict["ounce"] as! String) ounds =\(weightDict["gram"] as! String) grams")
            }
        }
    }
}



@IBAction func saveWeight(_ sender: Any) {

    if let savedWeights = UserDefaults.standard.object(forKey: "savedWeightConversion") as? [Any] {
        weightArray = savedWeights
    }
    weightDict = [:]
    weightDict.updateValue(textOunce.text, forKey: "ounce")
    weightDict.updateValue(textPound.text, forKey: "pound")
    weightDict.updateValue(textGram.text, forKey: "gram")
    weightArray.append(weightDict)


    let defaults = UserDefaults.standard

    defaults.set(weightArray,forKey: "savedWeightConversion")

    if let savedWeights = UserDefaults.standard.object(forKey: "savedWeightConversion") as? [Any] {
        for i in 0 ..< savedWeights.count{
            if let weightDict  = savedWeights[i] as? [String:Any] {
                print("\(weightDict["pound"] as! String) pounds= \(weightDict["ounce"] as! String) ounds =\(weightDict["gram"] as! String) grams")
            }
        }
    }

}