我已经检查所有的这些主题:结构数组:UserDefaults,如何使用?
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] = []
而在我的应用程序中,我在这些数组中动态添加了一些值。
没有为GET DATAS委托方法从我的第二个视图控制器
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]]
}
我需要的是存储两个阵列DATAS在UserDefaults(或CoreData可能?),当用户回到应用程序中使用这些DATAS。
我不知道该怎么做,我已经尝试了3个主题的方法,但我甚至都没有做好。
我该怎么办?
谢谢你们!
你链接的最后一篇文章解释得很好。你对此不了解什么? – Sweeper
如何使用它,我不明白这个方法。当调用保存和重载功能时。 – pierreafranck
您编写了一个将结构转换为'[String:Any]'并从中转换的方法。这样您可以将转换的结构保存到UserDefaults中。当您检索它时,将字典转换回结构。显示您已阅读的所有帖子中尝试过的内容。 – Sweeper