Created
June 15, 2020 10:19
-
-
Save amrangry/89097b86514b3477cae79dd28bba3f23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ValueWrapper.swift | |
// ADKATech.com | |
// | |
// Created by Amr Elghadban on 9/20/18. | |
// Copyright © 2018 Mobile DevOps. All rights reserved. | |
// | |
import Foundation | |
enum ValueWrapper: Codable { | |
case stringValue(String) | |
case intValue(Int) | |
case doubleValue(Double) | |
case boolValue(Bool) | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
if let value = try? container.decode(String.self) { | |
self = .stringValue(value) | |
return | |
} | |
if let value = try? container.decode(Bool.self) { | |
self = .boolValue(value) | |
return | |
} | |
if let value = try? container.decode(Double.self) { | |
self = .doubleValue(value) | |
return | |
} | |
if let value = try? container.decode(Int.self) { | |
self = .intValue(value) | |
return | |
} | |
throw DecodingError.typeMismatch(ValueWrapper.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ValueWrapper")) | |
} | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
switch self { | |
case let .stringValue(value): | |
try container.encode(value) | |
case let .boolValue(value): | |
try container.encode(value) | |
case let .intValue(value): | |
try container.encode(value) | |
case let .doubleValue(value): | |
try container.encode(value) | |
} | |
} | |
var rawValue: String { | |
var result: String | |
switch self { | |
case let .stringValue(value): | |
result = value | |
case let .boolValue(value): | |
result = String(value) | |
case let .intValue(value): | |
result = String(value) | |
case let .doubleValue(value): | |
result = String(value) | |
} | |
return result | |
} | |
var intValue: Int? { | |
var result: Int? | |
switch self { | |
case let .stringValue(value): | |
result = Int(value) | |
case let .intValue(value): | |
result = value | |
case let .boolValue(value): | |
result = value ? 1 : 0 | |
case let .doubleValue(value): | |
result = Int(value) | |
} | |
return result | |
} | |
var boolValue: Bool? { | |
var result: Bool? | |
switch self { | |
case let .stringValue(value): | |
result = Bool(value) | |
case let .boolValue(value): | |
result = value | |
case let .intValue(value): | |
result = Bool(truncating: value as NSNumber) | |
case let .doubleValue(value): | |
result = Bool(truncating: value as NSNumber) | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you so much, you safe my life ^^