结构数组:UserDefaults,如何使用?

时间:2017-08-21 08:35:04

标签: swift

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

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

  

我有一个包含一些字符串和另一个结构的结构: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

中有get数据的委托方法
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个主题的方法,但我甚至没有做好。

我该怎么做?

谢谢你们!

1 个答案:

答案 0 :(得分:2)

由于两种类型仅包含属性列表兼容类型,因此合适的解决方案是添加代码以将每种类型转换为符合属性列表的对象,反之亦然。

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)

修改

如果在所有其他情况下从propertyList获取数据,请使用UserDefaults初始值设定项

例如替换

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

基本上第一个创建对象,然后将其附加到数组。反过来非常麻烦。