arrays - swift 4 : how to write/load structure from file

标签 arrays swift save load structure

我是 Swift 的新手..(这是我在这里的第一篇文章!)

我将 Swift 4 与 Xcode 9 一起使用,我实际上是在尝试将包含结构的变量保存到文件中,然后再次将其重新加载到该变量中。

这是我要保存的数据:

var MyVar = [
structure(data1: "48.1", data2: "-10.1", Name: "aaa", Date: "2013-12-01 03:22:32", Age: 20),
structure(data1: "47.7", data2: "-79.2", Name: "bbb", Date: "2011-10-15 12:58:45", Age: 21),
structure(data1: "50.5", data2: "75.8", Name: "ccc", Date: "2014-05-24 02:42:25", Age: 35),
structure(data1: "48.7", data2: "78.5", Name: "ddd", Date: "2017-10-10 15:56:13", Age: 5)]

你能告诉我保存和重新加载这个变量的最简单方法吗?

最佳答案

  1. 使你的结构类型符合 Codable 协议(protocol)
  2. 使用PropertyListEncoder编码解码成属性列表
  3. 将该属性列表存储到文件系统。

参见 https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

关于arrays - swift 4 : how to write/load structure from file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043648/

相关文章:

c# - 将数据从 SQL Server 导出到 C# 中的文本文件(保存到特定文件夹)

android - 如何应用随机数字列表从数组中选择图像?

arrays - 为时间轴定义数组的更好方法,它可以更通用

swift 2.0 : Protocol extensions: Two protocols with the same function signature compile error

ios - 为什么必须将 View 属性分配给变量?

image - 时间:: reduced image quality when saving JPEG at 100% quality

c++ - 整数数组与字符数组

array.filter() 中的 javascript 字符串比较问题

javascript - 在 Acrobat Javascript 中从 XML 数据创建数组时遇到问题,如果可以的话请提供帮助

validation - 验证成功后,什么会导致 Grails GORM 保存失败?