如何解码保存并从userDeafults中读取的数组数据-Swift [英] How to decode data of an array that is save and read from userDeafults - Swift

查看:79
本文介绍了如何解码保存并从userDeafults中读取的数组数据-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,它有一个包含字典行的plist,然后,我将所有数据行都放到一个数组中,并将其保存到useDefaults中,所有这些看起来都正确,但是在read方法,据我了解,我的数据似乎已编码并且以代码形式显示。像这样的(6b756e74)我想知道如何以一种非常正确的方式解码我的数据。这是我的代码

I am working on a project that it has a plist contains of rows of dictionaries , and then , I put all the rows of datas to an array and I save it to the useDefaults and all these things seems correct , but in the read method , I understand that my data seems to be coded and it shows it in codes. like this (6b756e74) I wonder to know how I can decode my data in a very correct way. here is my codes

struct Country : Codable {
    let orFlagEmoji, destFlagEmoji : String

    private enum CointryKeys : String, CodingKey { case orFlagEmoji, destFlagEmoji }
}

var countries = [Country]()

  override func viewDidLoad() {
    super.viewDidLoad()


    let urlPlist = Bundle.main.url(forResource: "ListinFirstPage", withExtension: "plist")!
    let data = try! Data(contentsOf: urlPlist)


    do
    {
        countries = try PropertyListDecoder().decode([Country].self, from: data)
    }
    catch
    {
        // Handle error
        print(error)
    }
      savePlaces()
      readPlaces()
   }

   func savePlaces(){

    do
    {
        let placesData = try PropertyListEncoder().encode(countries)
        UserDefaults.standard.set(placesData , forKey: "places")

        return
    }
    catch
    {
    print("SaveError")
    }


}

func readPlaces()
{

    let loadedcart = UserDefaults.standard.object(forKey: "places")

    print(loadedcart as Any)

}

问题是,当我在控制台中打印 loadedcart (应该包含国家/地区数据)时,似乎已对数据进行了某种编码。输出是一些数字而不是我的字符串数据。如果有人帮助我改正,我将非常感谢。非常感谢

The problem is that when I print loadedcart (that it should contains countries data) in console it seems data has been coded some how. the output is some numbers not my string data. I really appreciate if some one help me to make it correct. Thank you very much

推荐答案

首先删除 readPlaces 。没意义

func readPlaces()
{   
    let loadedcart = UserDefaults.standard.object(forKey: "places")    
    print(loadedcart as Any)    
}

也删除

private enum CointryKeys : String, CodingKey { case orFlagEmoji, destFlagEmoji }

因为从未使用过键(甚至不正确)拼写为 CountryKeys )。

because the keys are never used (not even correctly spelled CountryKeys).

基本上是您的代码,用于加载和保存

Basically your code to load and save the data is correct, it's just misused.

检查是否在 UserDefaults中将键放置 为空。如果是这样,则从捆绑包中复制数据

Check if the key places in UserDefaults is empty. If so copy the data from the bundle

var countries = [Country]()

override func viewDidLoad() {
    super.viewDidLoad()
    do {
       try loadPlaces()
    } catch { print(error) }
}

func loadPlaces() throws 
{
    let data : Data
    if let placesData = UserDefaults.standard.data(forKey: "places") {
       data = placesData
    } else {
       let urlPlist = Bundle.main.url(forResource: "ListinFirstPage", withExtension: "plist")!
       data = try Data(contentsOf: urlPlist)
       UserDefaults.standard.set(data, forKey: "places")
    }
    countries = try PropertyListDecoder().decode([Country].self, from: data) 
}

func savePlaces()
{
    do {
        let placesData = try PropertyListEncoder().encode(countries)
        UserDefaults.standard.set(placesData , forKey: "places")
    } catch {
        print("SaveError", error)
    }
}

总是在修改<$后调用 savePlaces c $ c>国家(在 viewDidLoad中除外)

Call savePlaces always after having modified countries (except in viewDidLoad)

如评论中所述 UserDefaults 不是保存大量数据的正确位置。最好是 Documents 文件夹中的自定义文件。

As mentioned in the comments UserDefaults is not the right place to save a large set of data. A custom file in the Documents folder is preferable.

这篇关于如何解码保存并从userDeafults中读取的数组数据-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆