【发布时间】:2017-08-21 08:35:04
【问题描述】:
我已经检查了所有这些主题:
How to save an array of custom struct to NSUserDefault with swift?
How to save struct to NSUserDefaults in Swift 2.0
我有一个包含一些字符串和另一个结构的结构:MySection。
struct MySection {
var name: String = ""
var values: [MyRow] = []
}
还有 MyRow 存储在 MySection.values 中
struct MyRow {
var value: String = ""
var quantity: String = ""
var quantityType: String = ""
var done: String = ""
}
两个数组供使用
var arraySection: [MySection] = []
var arrayRow: [MyRow] = []
在我的应用程序中,我在这些数组中动态添加了一些值。
有从我的第二个 ViewController 获取数据的委托方法
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow())
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}
还有manageSection函数。
func manageSection(item: String) {
var i = 0
for _ in arraySection {
if arraySection[i].name == item {
arraySection.insert(MySection(), at: i + 1)
arraySection[i + 1].values = [arrayRow[arrayRow.count - 1]]
return
}
i += 1
}
arraySection.append(MySection())
arraySection[arraySection.count - 1].name = item
arraySection[arraySection.count - 1].values = [arrayRow[arrayRow.count - 1]]
}
我的需要是将两个数组的数据存储在 UserDefaults(或者可能是 CoreData ?)中,并在用户返回应用程序时使用这些数据。
我不知道该怎么做,我已经尝试了 3 个主题的方法,但我什至没有做好。
我该怎么做?
谢谢大家!
【问题讨论】:
-
你链接的最后一篇文章解释得很好。你有什么不明白的?
-
怎么用,没看懂方法。当调用保存和重新加载函数时..
-
您编写了一个将结构与
[String: Any]相互转换的方法。这样您就可以将转换后的结构保存到 UserDefaults 中。当您检索它时,您将字典转换回结构。从您阅读的所有这些帖子中展示您尝试过的东西。
标签: swift