2017-08-21 32 views
0

我已经检查所有的这些主题:结构数组:UserDefaults,如何使用?

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] = [] 

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

没有为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个主题的方法,但我甚至都没有做好。

我该怎么办?

谢谢你们!

+0

你链接的最后一篇文章解释得很好。你对此不了解什么? – Sweeper

+0

如何使用它,我不明白这个方法。当调用保存和重载功能时。 – pierreafranck

+0

您编写了一个将结构转换为'[String:Any]'并从中转换的方法。这样您可以将转换的结构保存到UserDefaults中。当您检索它时,将字典转换回结构。显示您已阅读的所有帖子中尝试过的内容。 – Sweeper

回答

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

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

+0

感谢您的回答。我尝试实现你的答案的第一部分,现在当我尝试添加一些东西在我的arraySection,我在这一行上有一个错误:'init(propertyList:[String:String]){ self.value = propertyList [“value”]!' – pierreafranck

+0

有两个初始化程序,一个标准的和一个第二个,它们需要一个带有与属性相对应的四个键的字典。第二个初始化程序应该仅用于使用'propertyListRepresentation'创建的对象,因为没有检查'nil'。 – vadian

+0

我编辑并发布了我的整个代码,你能帮助我吗?我不知道如何使它工作。 – pierreafranck