我有这样的结构:
struct JSONModelSettings {
let patientID : String
let therapistID : String
var isEnabled : Bool
enum CodingKeys: String, CodingKey {
case settings // The top level "settings" key
}
// The keys inside of the "settings" object
enum SettingsKeys: String, CodingKey {
case patientID = "patient_id"
case therapistID = "therapist_id"
case isEnabled = "is_therapy_forced"
}
}
extension JSONModelSettings: Decodable {
init(from decoder: Decoder) throws {
// Extract the top-level values ("settings")
let values = try decoder.container(keyedBy: CodingKeys.self)
// Extract the settings object as a nested container
let user = try values.nestedContainer(keyedBy: SettingsKeys.self, forKey: .settings)
// Extract each property from the nested container
patientID = try user.decode(String.self, forKey: .patientID)
therapistID = try user.decode(String.self, forKey: .therapistID)
isEnabled = try user.decode(Bool.self, forKey: .isEnabled)
}
}
和这种格式的 JSON(用于从设置中提取键的结构,没有额外的包装器):
{
"settings": {
"patient_id": "80864898",
"therapist_id": "78920",
"enabled": "1"
}
}
问题是如何将"isEnabled"转换为布尔值(从 API 获取 1 或 0) 当 im 尝试解析响应时,我收到错误: "本以为会解码布尔,但找到了一个数字。">
在这些情况下,我通常喜欢像 JSON 数据一样保留模型,所以在你的例子中是 Ints。比我向模型添加计算属性以转换为布尔值等
struct Model {
let enabled: Int
var isEnabled: Bool {
return enabled == 1
}
}
我的建议:不要与 JSON 战斗。尽快将其转换为 Swift 值,并尽可能少大惊小怪,然后在那里进行操作。
您可以定义一个私有内部结构来保存解码的数据,如下所示:
struct JSONModelSettings {
let patientID : String
let therapistID : String
var isEnabled : Bool
}
extension JSONModelSettings: Decodable {
// This struct stays very close to the JSON model, to the point
// of using snake_case for its properties. Since it's private,
// outside code cannot access it (and no need to either)
private struct JSONSettings: Decodable {
var patient_id: String
var therapist_id: String
var enabled: String
}
private enum CodingKeys: String, CodingKey {
case settings
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let settings = try container.decode(JSONSettings.self, forKey: .settings)
patientID = settings.patient_id
therapistID = settings.therapist_id
isEnabled = settings.enabled == "1"
}
}
其他JSON映射框架,如ObjectMapper,允许您将转换函数附加到编码/解码过程。看起来Codable
目前没有等价物。
属性包装器
要将String
s、Int
s、Double
s 或Bool
s 解码为Bool
,
只需将@SomeKindOfBool
放在布尔属性之前,如下所示:
@SomeKindOfBool public var someKey: Bool
<小时 />演示:
struct MyType: Decodable {
@SomeKindOfBool public var someKey: Bool
}
let jsonData = """
[
{ "someKey": "true" },
{ "someKey": "yes" },
{ "someKey": "1" },
{ "someKey": 1 },
{ "someKey": "false" },
{ "someKey": "no" },
{ "someKey": "0" },
{ "someKey": 0 }
]
""".data(using: .utf8)!
let decodedJSON = try! JSONDecoder().decode([MyType].self, from: jsonData)
for decodedType in decodedJSON {
print(decodedType.someKey)
}
这背后强大的PropertyWrapper实现:
@propertyWrapper
struct SomeKindOfBool: Decodable {
var wrappedValue: Bool
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
//Handle String value
if let stringValue = try? container.decode(String.self) {
switch stringValue.lowercased() {
case "false", "no", "0": wrappedValue = false
case "true", "yes", "1": wrappedValue = true
default: throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expect true/false, yes/no or 0/1 but`(stringValue)` instead")
}
}
//Handle Int value
else if let intValue = try? container.decode(Int.self) {
switch intValue {
case 0: wrappedValue = false
case 1: wrappedValue = true
default: throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expect `0` or `1` but found `(intValue)` instead")
}
}
//Handle Int value
else if let doubleValue = try? container.decode(Double.self) {
switch doubleValue {
case 0: wrappedValue = false
case 1: wrappedValue = true
default: throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expect `0` or `1` but found `(doubleValue)` instead")
}
}
else {
wrappedValue = try container.decode(Bool.self)
}
}
}
如果您需要实现一个可选的,请在此处查看此答案
现在是2021 年,我们有更简单的方法在 Swift 5 中使用 PropertyWrapper 解决这个问题。
@propertyWrapper
struct BoolFromInt: Decodable {
var wrappedValue: Bool // or use `let` to make it immutable
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let intValue = try container.decode(Int.self)
switch intValue {
case 0: wrappedValue = false
case 1: wrappedValue = true
default: throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expected `0` or `1` but received `(intValue)`")
}
}
}
用法:
struct Settings: Decodable {
@BoolFromInt var isEnabled: Bool
}
解码为String
,然后将其转换为Bool
,只需修改代码的某些行:
("0"
是 JSON 字符串,不能解码为Int
。
struct JSONModelSettings {
let patientID : String
let therapistID : String
var isEnabled : Bool
enum CodingKeys: String, CodingKey {
case settings // The top level "settings" key
}
// The keys inside of the "settings" object
enum SettingsKeys: String, CodingKey {
case patientID = "patient_id"
case therapistID = "therapist_id"
case isEnabled = "enabled"//### "is_therapy_forced"?
}
}
extension JSONModelSettings: Decodable {
init(from decoder: Decoder) throws {
// Extract the top-level values ("settings")
let values = try decoder.container(keyedBy: CodingKeys.self)
// Extract the settings object as a nested container
let user = try values.nestedContainer(keyedBy: SettingsKeys.self, forKey: .settings)
// Extract each property from the nested container
patientID = try user.decode(String.self, forKey: .patientID)
therapistID = try user.decode(String.self, forKey: .therapistID)
//### decode the value for "enabled" as String
let enabledString = try user.decode(String.self, forKey: .isEnabled)
//### You can throw type mismatching error when `enabledString` is neither "0" or "1"
if enabledString != "0" && enabledString != "1" {
throw DecodingError.typeMismatch(Bool.self, DecodingError.Context(codingPath: user.codingPath + [SettingsKeys.isEnabled], debugDescription: "value for "enabled" needs to be "0" or "1""))
}
//### and convert it to Bool
isEnabled = enabledString != "0"
}
}