Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let others : [String: OthersType?]?
- enum OthersType: Codable {
- case int(Int)
- case string(String)
- case bool(Bool)
- init(from decoder: Decoder) throws {
- let container = try decoder.singleValueContainer()
- do {
- self = try .int(container.decode(Int.self))
- } catch DecodingError.typeMismatch {
- do {
- self = try .string(container.decode(String.self))
- } catch DecodingError.typeMismatch {
- do {
- self = try .bool(container.decode(Bool.self))
- } catch DecodingError.typeMismatch {
- throw DecodingError.typeMismatch(OthersType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Encoded payload not of an expected type"))
- }
- }
- }
- }
- func encode(to encoder: Encoder) throws {
- var container = encoder.singleValueContainer()
- switch self {
- case .int(let int):
- try container.encode(int)
- case .string(let string):
- try container.encode(string)
- case .bool(let bool):
- try container.encode(bool)
- }
- }
- func getValue(_ ofTheWord: OthersType) -> String {
- var result = String(describing: ofTheWord)
- if result.contains("string(") {
- result = result.replacingOccurrences(of: "string("", with: "")
- result.removeLast(2)
- } else if result.contains("int(") {
- result = result.replacingOccurrences(of: "int(", with: "")
- result.removeLast(1)
- } else if result.contains("bool(") {
- result = result.replacingOccurrences(of: "bool(", with: "")
- result.removeLast(1)
- }
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement