【问题标题】:Swift: Retrieving UserDefaults dictionary data into labelsSwift:将 UserDefaults 字典数据检索到标签中
【发布时间】:2018-08-27 00:05:30
【问题描述】:

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

x 磅= x 磅 =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]()


    }

【问题讨论】:

    标签: ios swift dictionary nsuserdefaults


    【解决方案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")
                }
            }
        }
    
    }
    

    【讨论】:

    • 这行得通。但它会覆盖以前的值并替换为新值。如何通过保留以前的值来做到这一点
    • 我将多次输入值并保存它。所以每次输入值时根据此代码它会覆盖旧值并保存新值。我也想显示旧值.并且新值应该显示在保存的旧值之后。
    【解决方案2】:

    你可以试试

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

    【讨论】:

    • 当应用加载到viewDidAppear方法中的标签中时如何加载回
    猜你喜欢
    • 2017-03-27
    • 2018-10-03
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2016-08-19
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多