decodeIfPresent if nil
This commit is contained in:
parent
12e06894de
commit
5d836fc757
@ -45,10 +45,14 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
||||
|
||||
//MARK: - DecodeIfPresent
|
||||
public func decodeIfPresent<T, C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T? {
|
||||
|
||||
//get the type string
|
||||
let meta = try self.nestedContainer(keyedBy: C.self, forKey: codingKey)
|
||||
guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
||||
var me: KeyedDecodingContainer<C>?
|
||||
do {
|
||||
me = try self.nestedContainer(keyedBy: C.self, forKey: codingKey)
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
guard let type = try me?.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user