Merge branch 'feature/decoder_delegate' into 'develop'

delegate object for model decoding functions

See merge request BPHV_MIPS/mvm_core!82
This commit is contained in:
Pfeil, Scott Robert 2020-05-12 09:21:06 -04:00
commit e5e46662a6

View File

@ -61,4 +61,26 @@ extension Decodable {
}
}
public enum DecoderKeyError: Error {
case createKey
}
public extension JSONDecoder {
/// Adds a delegate object to the decoder for use in the model initializers.
func add<T: DelegateObject>(delegateObject: T) throws {
guard let key = CodingUserInfoKey(rawValue: "delegateObject") else {
throw DecoderKeyError.createKey
}
userInfo.updateValue(delegateObject, forKey: key)
}
}
public extension Decoder {
/// Gets a delegate object from the decoder for use in the model initializers. Had to be added before decode.
func get<T: DelegateObject>() throws -> T? {
guard let key = CodingUserInfoKey(rawValue: "delegateObject") else {
throw DecoderKeyError.createKey
}
return userInfo[key] as? T
}
}