I get json from server and I need to convert id field to string in swift code.
The problem is json sometimes returns "12345"
, sometimes returns 12345
(with or without quotes).
Is it possible to resolve this issue without of checking the value type and checking if the conversion result is nil
?
UPDATED
Example of code I use with checking conversion result:
let result = (some_index as? String) ?? String(some_index as! Int)
The problem is in objective-C you have [NSString stringWithFormat:@"%@", some_object]
. But in swift you have optionals and it tries to insert word "optional" into result.
UPDATED
STOP spam with random answers about optionals. The question is concrete - "how to simply unwrap json value which may look like String, Int or doesn't exist at all?"
swift How to remove optional String Character
In this question they ask how to convert Int? -> Int
, String? -> String
and similar. In my case I don't know if I have Int?
or String?
as the initial type.