用Swift编码一个浮点数的结构4(Making a Struct with a Float encodable with Swift 4)

编程入门 行业动态 更新时间:2024-10-20 05:44:09
用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 objects

I'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

更多推荐

本文发布于:2023-07-04 17:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构   浮点数   Swift   Making   encodable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!