gpt4 book ai didi

swift - Array of structs : UserDefaults,如何使用?

转载 作者:搜寻专家 更新时间:2023-11-01 07:09:31 25 4
gpt4 key购买 nike

我已经检查了所有这些主题:

How to save an array of custom struct to NSUserDefault with swift?

How to save struct to NSUserDefaults in Swift 2.0

STRUCT Array To UserDefaults

I have a struct containing some Strings and an other struct: MySection.

struct MySection {
var name: String = ""
var values: [MyRow] = []
}

And there is MyRow which is store in MySection.values

struct MyRow {
var value: String = ""
var quantity: String = ""
var quantityType: String = ""
var done: String = ""
}

Two arrays for use it

var arraySection: [MySection] = []
var arrayRow: [MyRow] = []

在我的应用程序中,我在这些数组中动态添加了一些值。

There is the delegate method for get datas from my second ViewController

func returnInfos(newItem: [MyRow], sectionPick: String) {
arrayRow.append(MyRow())
arrayRow[arrayRow.count - 1] = newItem[0]
manageSection(item: sectionPick)
listTableView.reloadData()
}

And there is the manageSection function.

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()
}

基本上首先创建对象,然后将其附加到数组。反过来就很麻烦了。

关于swift - Array of structs : UserDefaults,如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45792601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com