MFViewController Model

Dynamic Root Non Root navigation item
This commit is contained in:
Pfeil, Scott Robert 2020-10-07 15:36:01 -04:00
parent ae94a2f197
commit b00201b1ab

View File

@ -16,8 +16,12 @@ extension JSONDecoder: AnyDecoder {}
extension PropertyListDecoder: AnyDecoder {} extension PropertyListDecoder: AnyDecoder {}
extension Data { extension Data {
public func decode<T: Decodable>(using decoder: AnyDecoder = JSONDecoder()) throws -> T { public func decode<T: Decodable>(using decoder: AnyDecoder = JSONDecoder(), delegateObject: DelegateObject? = nil) throws -> T {
return try decoder.decode(T.self, from: self) if let decoder = decoder as? JSONDecoder {
return try decoder.decode(T.self, from: self, delegateObject: delegateObject)
} else {
return try decoder.decode(T.self, from: self)
}
} }
} }
@ -35,10 +39,10 @@ extension KeyedDecodingContainerProtocol {
} }
extension Decodable { extension Decodable {
public static func decode(jsonDict: [String: Any]) throws -> Self { public static func decode(jsonDict: [String: Any], delegateObject: DelegateObject? = nil) throws -> Self {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDict) let jsonData = try JSONSerialization.data(withJSONObject: jsonDict)
do { do {
return try jsonData.decode() return try jsonData.decode(delegateObject: delegateObject)
} catch { } catch {
throw JSONError.other(error: error) throw JSONError.other(error: error)
} }
@ -73,6 +77,14 @@ public extension JSONDecoder {
} }
userInfo.updateValue(delegateObject, forKey: key) userInfo.updateValue(delegateObject, forKey: key)
} }
/// Decodes a top-level value of the given type from the given JSON representation, and adds the delegate object if provided.
func decode<T>(_ type: T.Type, from data: Data, delegateObject: DelegateObject?) throws -> T where T : Decodable {
if let delegateObject = delegateObject {
try add(delegateObject: delegateObject)
}
return try decode(T.self, from: data)
}
} }
public extension Decoder { public extension Decoder {