Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 将结构数组转换为用户默认值_Swift_Struct_Nsarray_Nsuserdefaults - Fatal编程技术网

Swift 将结构数组转换为用户默认值

Swift 将结构数组转换为用户默认值,swift,struct,nsarray,nsuserdefaults,Swift,Struct,Nsarray,Nsuserdefaults,我有一个自定义的Struct类来保存卡路里、脂肪、碳水化合物和蛋白质 每次用户输入数据时,我都将其放入变量中 var theArray : NSMutableArray = [] struct CFCPstruct { let calories : Int! let fats : Int! let carbs : Int! let protein: Int! init(Calories: Int, Fats: Int, Carbs: Int, Prot

我有一个自定义的Struct类来保存卡路里、脂肪、碳水化合物和蛋白质

每次用户输入数据时,我都将其放入变量中

 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)]
现在我还创建了一个数组来存储所有内容。然后我需要将所有值存储到数组中,然后数组将其存储到UserDefaults


然后我需要调用用户默认调用数组[0],比如说,然后调用每个卡路里、碳水化合物。。。类似于log.carries//thiology.carbs等的东西要能够使用
NSCoding
对象必须是类。但由于所有值都与属性列表兼容,您可以添加变量
dictionaryRepresentation
和相应的初始值设定项

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]
    }
}
首先,never在Swift中使用
NSMutableArray
,并never将变量声明为隐式展开可选变量,该变量使用非可选初始值设定项初始化

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]
    }
}
现在您可以从用户默认值读取和写入数组

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

现在,如果已经创建了数据数组,则可以将其保存为:-

让你的排列信息=[[a:“史密斯”,“b:“英国”],[a:“保罗”,“b”: “阿联酋”]]

Swift 3:

let defaults = UserDefaults.standard
defaults.set(yourArrayOfDict, forKey: "customKey_SavedArray")
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(yourArrayOfDict, forKey: "customKey_SavedArray")
现在,您可以通过以下方式访问它们:

您将获得s中的值,因此您可以
打印
,以确保保存数组

Swift 2:

let defaults = UserDefaults.standard
defaults.set(yourArrayOfDict, forKey: "customKey_SavedArray")
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(yourArrayOfDict, forKey: "customKey_SavedArray")
访问它们:

现在,这里的s将值作为数据数组保存,因此,正如您所说,您不能使用,
让第一项=s[0]
, 或者,您可以使用
for in
循环遍历数组中的项