用Swift编码一个浮点数的结构4(Making a Struct with a Float encodable with Swift 4)
我有一个包含人物(身高,年龄,性别)的描述的结构。 年龄是一个浮动,因为它可以在几个月内计算婴儿(即:1.083年是13个月)。
我试图将这些人的数组保存为用户默认值,但无法弄清楚如何让该类可编码。 在类中是Strings,一个Float和一个Dictionary [String:String]。
建议?
结构:
import Foundation struct Person: Codable { var name: String var gender: String var age: Float var profilePictures: [String: String] init(name: String, gender: String, age: Float, profilePictures: [String: String]) { self.name = name self.gender = gender self.age = age self.profilePictures = profilePictures } }我尝试将结果保存到用户默认值的类:
let defaults = UserDefaults.standard defaults.set(self.people, forKey: "people") // Where self.people is an array of Person objectsI've got a struct that contains descriptions of People (height, age, gender). Age is a Float because it could be counted in months for infants (ie: 1.083 years is 13 months).
I'm trying to save an array of these People to User Defaults, but can't figure out how to make the class codable. In the class is Strings, one Float, and a Dictionary [String:String].
Suggestions?
Struct:
import Foundation struct Person: Codable { var name: String var gender: String var age: Float var profilePictures: [String: String] init(name: String, gender: String, age: Float, profilePictures: [String: String]) { self.name = name self.gender = gender self.age = age self.profilePictures = profilePictures } }Class where I try to save the results to User Defaults:
let defaults = UserDefaults.standard defaults.set(self.people, forKey: "people") // Where self.people is an array of Person objects更多推荐
发布评论