简体   繁体   English

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

[英]Swift: Retrieving UserDefaults dictionary data into labels

I am new to swift programming. 我是快速编程的新手。 I am saving some text field data into a dictionary as UserDefaults. 我正在将一些文本字段数据作为UserDefaults保存到字典中。 I need to retrieve these data back and display it in the following format. 我需要检索这些数据并以以下格式显示。

x pounds= x ounds =x grams x磅= x磅= x克

This the code where I have saved it into UserDefaults in a dictionary. 这是我将其保存到字典的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)
        }

    }

To view it on application load 在应用程序加载时查看

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


    }

You can try 你可以试试

  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

            }

        }

    }

try this 尝试这个

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")
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM