Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ExtractedData.swift
- // AGKitDemo-Template
- //
- // Created by Anthony on 20/03/2018.
- // Copyright © 2018 AboutGoods. All rights reserved.
- //
- import Foundation
- protocol ExtractedDataProtocol {
- func getBestValue() -> Any
- }
- struct ExtractedData {
- var value:Any?
- var accuracy:Double?
- enum CodingKeys: String, CodingKey {
- case value = "value"
- case accuracy
- }
- }
- extension ExtractedData: Encodable {
- func encode(to encoder: Encoder) throws {
- var container = encoder.container(keyedBy: CodingKeys.self)
- if let doubleValue = self.value as? Double {
- try container.encode(doubleValue, forKey: .value)
- } else if let stringValue = self.value as? String {
- try container.encode(stringValue, forKey: .value)
- } else {
- let context = DecodingError.Context(codingPath: [CodingKeys.value], debugDescription: "Unrecognized type for value")
- throw DecodingError.valueNotFound(ExtractedData.self, context)
- }
- try container.encode(self.accuracy, forKey: .accuracy)
- }
- }
- extension ExtractedData: Decodable {
- init(from decoder: Decoder) throws {
- let values = try decoder.container(keyedBy: CodingKeys.self)
- if let decodedValue = try? values.decode(Double.self, forKey: .value) {
- self.value = decodedValue
- } else if let decodedValue = try? values.decode(String.self, forKey: .value){
- self.value = decodedValue
- } else {
- let context = DecodingError.Context(codingPath: [CodingKeys.value], debugDescription: "No concording type found for value")
- throw DecodingError.valueNotFound(ExtractedData.self, context)
- }
- self.accuracy = try values.decode(Double.self, forKey: .accuracy)
- }
- }
Add Comment
Please, Sign In to add comment