我已经检查了所有这些主题:
如何使用 swift 将自定义结构数组保存到 NSUserDefault?
如何在 Swift 2.0 中将结构保存到 NSUserDefaults
结构数组到用户默认值
我有一个包含一些字符串的结构和另一个结构: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] = []
在我的应用程序中,我在这些数组中动态添加一些值。
有一个委托方法,用于从我的第二个视图控制器获取数据
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow())
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}
还有管理部分功能。
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 个主题的方法,但我什至没有做得很好。
我该怎么做?
谢谢大家!
由于这两种类型都只包含与属性列表兼容的类型,因此合适的解决方案是添加代码以将每种类型转换为符合属性列表的对象,反之亦然。
struct MySection {
var name: String
var values = [MyRow]()
init(name : String, values : [MyRow] = []) {
self.name = name
self.values = values
}
init(propertyList: [String: Any]) {
self.name = propertyList["name"] as! String
self.values = (propertyList["values"] as! [[String:String]]).map{ MyRow(propertyList: $0) }
}
var propertyListRepresentation : [String: Any] {
return ["name" : name, "values" : values.map { $0.propertyListRepresentation }]
}
}
struct MyRow {
var value: String
var quantity: String
var quantityType: String
var done: String
init(value : String, quantity: String, quantityType: String, done: String) {
self.value = value
self.quantity = quantity
self.quantityType = quantityType
self.done = done
}
init(propertyList: [String:String]) {
self.value = propertyList["value"]!
self.quantity = propertyList["quantity"]!
self.quantityType = propertyList["quantityType"]!
self.done = propertyList["done"]!
}
var propertyListRepresentation : [String: Any] {
return ["value" : value, "quantity" : quantity, "quantityType" : quantityType, "done" : done ]
}
}
创建几个对象后
let row1 = MyRow(value: "Foo", quantity: "10", quantityType: "Foo", done: "Yes")
let row2 = MyRow(value: "Bar", quantity: "10", quantityType: "Bar", done: "No")
let section = MySection(name: "Baz", values: [row1, row2])
调用propertyListRepresentation
以获取可以保存到用户默认值的字典([String:Any]
(。
let propertyList = section.propertyListRepresentation
该部分的重新创建也很容易
let newSection = MySection(propertyList: propertyList)
编辑
仅当从UserDefaults
获取数据时,才使用propertyList
初始值设定项,在所有其他情况下使用其他初始值设定项。
例如替换
@IBAction func addButtonPressed(_ sender: Any) {
newProducts.append(MyRow(propertyList: ["":""]))
newProducts[newProducts.count - 1].value = nameTextField.text!
newProducts[newProducts.count - 1].quantity = quantityTextField.text!
newProducts[newProducts.count - 1].quantityType = type
newProducts[newProducts.count - 1].done = "No"
delegate?.returnInfos(newItem: newProducts, sectionPick: typePick)
navigationController?.popViewController(animated: true)
}
跟
@IBAction func addButtonPressed(_ sender: Any) {
let row = MyRow(value: nameTextField.text!,
quantity: quantityTextField.text!,
quantityType: type,
done: "No")
newProducts.append(row)
delegate?.returnInfos(newItem: newProducts, sectionPick: typePick)
navigationController?.popViewController(animated: true)
}
并替换
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow(propertyList: ["":""]))
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}
跟
func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(newItem[0])
manageSection(item: sectionPick)
listTableView.reloadData()
}
基本上首先创建对象,然后将其附加到数组中。反之则非常麻烦。