STRUCT Array To UserDefaults

狂风中的少年 提交于 2019-12-11 07:13:56

问题


I have a custom Struct class to hold calories, fats, carbs, and protein.

Each Time a user enters the data I put it into a variable

 var theArray : NSMutableArray = []

struct CFCPstruct {
    let calories : Int!
    let fats : Int!
    let carbs : Int!
    let protein: Int!
    init(Calories: Int, Fats: Int, Carbs: Int, Protein: Int) {
        self.calories = Calories
        self.fats = Fats
        self.carbs = Carbs
        self.protein = Protein
    }
}

 let addLog = [CFCPstruct(Calories: totalCalories, Fats: totalFats, Carbs: totalCarbs, Protein: totalProtein)]

Now I also created an array to store everything. I then need to store all the values into array, which then store that to UserDefaults.

Then I will need to call the user defaults call array[0] lets say and then call each calorie, carb, ... something like thelog.calories // theology.carbs etc


回答1:


To be able to use NSCoding the object must be a class. But as all values are property list compliant you could add a variable dictionaryRepresentation and a corresponding initializer.

First of all never use NSMutableArray in Swift and never declare variables as implicit unwrapped optional which are initialized with a non-optional initializer.

var theArray = [CFCPstruct]()

struct CFCPstruct  {
    let calories : Int
    let fats : Int
    let carbs : Int
    let protein: Int

    init(calories: Int, fats: Int, carbs: Int, protein: Int) {
        self.calories = calories
        self.fats = fats
        self.carbs = carbs
        self.protein = protein
    }

    init(dictionary : [String:Int]) {
        self.calories = dictionary["calories"]!
        self.fats = dictionary["fats"]!
        self.carbs = dictionary["carbs"]!
        self.protein = dictionary["protein"]!
    }

    var dictionaryRepresentation : [String:Int] {
        return ["calories" : calories, "fats" : fats, "carbs" : carbs, "protein" : protein]
    }
}

Now you can read the array from and write to user defaults.

func saveDefaults() 
{
    let cfcpArray = theArray.map{ $0.dictionaryRepresentation }
    UserDefaults.standard.set(cfcpArray, forKey: "cfcpArray")
}

func loadDefaults()
{
    theArray = (UserDefaults.standard.object(forKey: "cfcpArray") as! [[String:Int]]).map{ CFCPstruct(dictionary:$0) }
}



回答2:


Now, if you have created an array of your data, you can save it as follow:-

let yourArrayOfDict = [["a": "Smith", "b": "UK"], ["a": "Paul", "b": "UAE"]]

Swift 3:

let defaults = UserDefaults.standard
defaults.set(yourArrayOfDict, forKey: "customKey_SavedArray")

Now, you access them by:

let s = defaults.value(forKey: "customKey_SavedArray")
print(s)

You will get your value in s, so you can print(s) to ensure the array is saved.

Swift 2:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(yourArrayOfDict, forKey: "customKey_SavedArray")

Access them :

let s = defaults.objectForKey("customKey_SavedArray") as? [[String:AnyObject]]

Now, the s here holds the value as an array of your data, so no you can access them using as you said, let first_item = s[0], Or, you can loop through the items in array using for in loop.



来源:https://stackoverflow.com/questions/42777752/struct-array-to-userdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!