├── Number.swift └── README.md /Number.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Number.swift 3 | // hhfa 4 | // 5 | // Created by hhfa on 11/01/2018. 6 | // Copyright © 2018 hhfa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | struct Number:Decodable { 12 | var intValue:Int 13 | var stringValue:String 14 | init(from decoder: Decoder) throws { 15 | let singleValueContainer = try decoder.singleValueContainer() 16 | 17 | if let stringValue = try? singleValueContainer.decode(String.self) 18 | { 19 | if let intValue = Int(stringValue) 20 | { 21 | self.intValue = intValue 22 | self.stringValue = String(intValue); 23 | } else 24 | { 25 | self.intValue = 0 26 | self.stringValue = String(0); 27 | } 28 | 29 | } else if let stringValue = try? singleValueContainer.decode(Int.self) 30 | { 31 | self.intValue = stringValue 32 | self.stringValue = String(stringValue); 33 | } else 34 | { 35 | self.intValue = 0 36 | self.stringValue = String(0); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NumberCodable 2 | --------------------------------------------------------------------------------