使用 Decodable 进行 JSON 解析时,optional 和 decodeIfPresent 有什么区别?
What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?
我第一次使用 Swift 4 的 Codable
协议,我无法理解 Decodable
的 decodeIfPresent
的用法。
/// Decodes a value of the given type for the given key, if present.
///
/// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call.
///
/// - parameter type: The type of value to decode.
/// - parameter key: The key that the decoded value is associated with.
/// - returns: A decoded value of the requested type, or `nil` if the `Decoder` does not have an entry associated with the given key, or if the value is a null value.
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type.
public func decodeIfPresent(_ type: String.Type, forKey key: KeyedDecodingContainer.Key) throws -> String?
此处建议它 returns nil
,如果关联键不存在值。如果这是唯一的原因,那么它与可选 属性 有何不同,因为如果响应中不存在值,可选变量也会设置为 nil
。
是的,@Sweeper 的评论很有道理。
我尽量按照自己的理解来解释。
public class User : Decodable{
public var firstName:String
public var lastName:String
public var middleName:String?
public var address:String
public var contactNumber:String
public enum UserResponseKeys: String, CodingKey{
case firstName = "first_name"
case lastName = "last_name"
case middleName = "middle_name"
case address = "address"
case contactNumber = "contact_number"
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: UserResponseKeys.self)
self.firstName = try container.decode(String.self, forKey: .firstName)
self.lastName = try container.decode(String.self, forKey: .lastName)
self.middleName = try container.decodeIfPresent(String.self, forKey: .middleName)
self.address = try container.decode(String.self, forKey: .address)
self.contactNumber = try container.decode(String.self, forKey: .contactNumber)
}
}
以上是我的User
class,其中我将middleName
标记为可选参数,因为JSON响应可能不提供middleName
] 键值对响应,所以我们可以使用 decodeIfPresent
.
self.middleName = try container.decodeIfPresent(String.self, forKey: .middleName)
而对于其他变量是必填字段,因此我们确信不需要为此使用可选字段。我们只使用 decode
因为该方法不是 return 可选的。
public func decode(_ type: String.Type, forKey key: KeyedDecodingContainer.Key) throws -> String
上面的decode
函数returns String
while decodeIfPresent
returns String?
,所以我们可以使用可选变量来存储它.
所以最后的结论是,如果您不确定服务响应合同,或者您可能会处理任何第三方服务,其中 JSON 响应和参数可能会在您不知情的情况下发生变化,那么您可以使用 decodeIfPresent
以便它可以处理响应中缺少特定参数并将值设置为 nil
.
这两行代码之间存在细微但重要的区别:
// Exhibit 1
foo = try container.decode(Int?.self, forKey: .foo)
// Exhibit 2
foo = try container.decodeIfPresent(Int.self, forKey: .foo)
图表 1 将解析:
{
"foo": null,
"bar": "something"
}
但不是:
{
"bar": "something"
}
而展览 2 将愉快地解析两者。因此,在 JSON
解析器的正常用例中,您需要 decodeIfPresent
用于模型中的每个可选项。
我认为使用 decodeifPresent
而不是可选的 属性 是有意义的,如果你想为 [=89] 中可能缺少的 属性 使用默认值=].
例如,让我们检查 3 种情况:
1.所有键都存在于 JSON:
假设您必须对此进行解码 JSON:
{
"project_names": ["project1", "project2", "project3"],
"is_pro": true
}
你可以使用这个结构:
struct Program: Codable {
let projectNames: [String]
let isPro: Bool
}
您将得到一个 Program
对象,其 isPro
值等于 true
。
(我想你的解码器 keyDecodingStrategy
在这个例子的其余部分是 .convertFromSnakeCase
)
2。 JSON 中缺少一些键,您可以在 Swift 中有一个可选键:
{
"project_names": ["project1", "project2", "project3"]
}
您现在可以使用这个结构:
struct Program: Codable {
let projectNames: [String]
var isPro: Bool?
}
你将得到一个 Program
对象,其 isPro
值等于 nil
.
如果 JSON 看起来像这样:
{
"project_names": ["project1", "project2", "project3"],
"is_pro": true
}
那么 isPro
将是一个 Bool?
,值为 true
。
也许这就是您想要的,但您可能想要一个默认值为 false
的 Bool
。这就是 decodeIfPresent
可能有用的地方。
3。 JSON 中缺少某些键,您想要 non-optional 属性 中的默认值 Swift:
如果您的结构如下所示:
struct Program: Codable {
let projectNames: [String]
var isPro: Bool = false
}
然后如果“is_pro”属性不在您的 JSON 中,您将得到一个解析错误。 因为 Codable 期望找到一个值来解析 Bool 属性。
在那种情况下,一个好主意是使用 decodeIfPresent
进行初始化,如下所示:
struct Program: Codable {
let projectNames: [String]
let isPro: Bool
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.projectNames = try container.decode([String].self, forKey: .projectNames)
self.isPro = try container.decodeIfPresent(Bool.self, forKey: .isPro) ?? false
}
}
这让您可以两全其美:
- 你的结构有一个
Bool
,而不是 Bool?
属性
- 您仍然可以解析不包含“is_pro”字段的 JSON
- 如果 JSON 中不存在该字段,您可以获得默认值
false
。
我第一次使用 Swift 4 的 Codable
协议,我无法理解 Decodable
的 decodeIfPresent
的用法。
/// Decodes a value of the given type for the given key, if present.
///
/// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call.
///
/// - parameter type: The type of value to decode.
/// - parameter key: The key that the decoded value is associated with.
/// - returns: A decoded value of the requested type, or `nil` if the `Decoder` does not have an entry associated with the given key, or if the value is a null value.
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type.
public func decodeIfPresent(_ type: String.Type, forKey key: KeyedDecodingContainer.Key) throws -> String?
此处建议它 returns nil
,如果关联键不存在值。如果这是唯一的原因,那么它与可选 属性 有何不同,因为如果响应中不存在值,可选变量也会设置为 nil
。
是的,@Sweeper 的评论很有道理。
我尽量按照自己的理解来解释。
public class User : Decodable{
public var firstName:String
public var lastName:String
public var middleName:String?
public var address:String
public var contactNumber:String
public enum UserResponseKeys: String, CodingKey{
case firstName = "first_name"
case lastName = "last_name"
case middleName = "middle_name"
case address = "address"
case contactNumber = "contact_number"
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: UserResponseKeys.self)
self.firstName = try container.decode(String.self, forKey: .firstName)
self.lastName = try container.decode(String.self, forKey: .lastName)
self.middleName = try container.decodeIfPresent(String.self, forKey: .middleName)
self.address = try container.decode(String.self, forKey: .address)
self.contactNumber = try container.decode(String.self, forKey: .contactNumber)
}
}
以上是我的User
class,其中我将middleName
标记为可选参数,因为JSON响应可能不提供middleName
] 键值对响应,所以我们可以使用 decodeIfPresent
.
self.middleName = try container.decodeIfPresent(String.self, forKey: .middleName)
而对于其他变量是必填字段,因此我们确信不需要为此使用可选字段。我们只使用 decode
因为该方法不是 return 可选的。
public func decode(_ type: String.Type, forKey key: KeyedDecodingContainer.Key) throws -> String
上面的decode
函数returns String
while decodeIfPresent
returns String?
,所以我们可以使用可选变量来存储它.
所以最后的结论是,如果您不确定服务响应合同,或者您可能会处理任何第三方服务,其中 JSON 响应和参数可能会在您不知情的情况下发生变化,那么您可以使用 decodeIfPresent
以便它可以处理响应中缺少特定参数并将值设置为 nil
.
这两行代码之间存在细微但重要的区别:
// Exhibit 1
foo = try container.decode(Int?.self, forKey: .foo)
// Exhibit 2
foo = try container.decodeIfPresent(Int.self, forKey: .foo)
图表 1 将解析:
{
"foo": null,
"bar": "something"
}
但不是:
{
"bar": "something"
}
而展览 2 将愉快地解析两者。因此,在 JSON
解析器的正常用例中,您需要 decodeIfPresent
用于模型中的每个可选项。
我认为使用 decodeifPresent
而不是可选的 属性 是有意义的,如果你想为 [=89] 中可能缺少的 属性 使用默认值=].
例如,让我们检查 3 种情况:
1.所有键都存在于 JSON:
假设您必须对此进行解码 JSON:
{
"project_names": ["project1", "project2", "project3"],
"is_pro": true
}
你可以使用这个结构:
struct Program: Codable {
let projectNames: [String]
let isPro: Bool
}
您将得到一个 Program
对象,其 isPro
值等于 true
。
(我想你的解码器 keyDecodingStrategy
在这个例子的其余部分是 .convertFromSnakeCase
)
2。 JSON 中缺少一些键,您可以在 Swift 中有一个可选键:
{
"project_names": ["project1", "project2", "project3"]
}
您现在可以使用这个结构:
struct Program: Codable {
let projectNames: [String]
var isPro: Bool?
}
你将得到一个 Program
对象,其 isPro
值等于 nil
.
如果 JSON 看起来像这样:
{
"project_names": ["project1", "project2", "project3"],
"is_pro": true
}
那么 isPro
将是一个 Bool?
,值为 true
。
也许这就是您想要的,但您可能想要一个默认值为 false
的 Bool
。这就是 decodeIfPresent
可能有用的地方。
3。 JSON 中缺少某些键,您想要 non-optional 属性 中的默认值 Swift:
如果您的结构如下所示:
struct Program: Codable {
let projectNames: [String]
var isPro: Bool = false
}
然后如果“is_pro”属性不在您的 JSON 中,您将得到一个解析错误。 因为 Codable 期望找到一个值来解析 Bool 属性。
在那种情况下,一个好主意是使用 decodeIfPresent
进行初始化,如下所示:
struct Program: Codable {
let projectNames: [String]
let isPro: Bool
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.projectNames = try container.decode([String].self, forKey: .projectNames)
self.isPro = try container.decodeIfPresent(Bool.self, forKey: .isPro) ?? false
}
}
这让您可以两全其美:
- 你的结构有一个
Bool
,而不是Bool?
属性 - 您仍然可以解析不包含“is_pro”字段的 JSON
- 如果 JSON 中不存在该字段,您可以获得默认值
false
。