Arrow icon indicating copy to clipboard operation
Arrow copied to clipboard

Cannot cast json to other type

Open christophedebatz opened this issue 7 years ago • 3 comments

Hi,

I'm wondering me to how I can cast the json values!

createdUser.id = Int32((json["id"]?.data as? Int32)!)

does not work.

id <-- Int32((json["id"]?.data as? Int32)!)

does not work.

id <--json["id"]

does not work.

Each time I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User setId:]: unrecognized selector sent to instance 0x60400047d100'

So do you have an idea about that? I'm surprised that nobody got the present error by the past :) My rest service returns id (so Int32 for me on swift) and some dates that I have to parse.

christophedebatz avatar Dec 31 '17 00:12 christophedebatz

Hello @christophedebatz,

Looks like you have Core Data model and there's a problem with it, rather than JSON parsing. The crash log says that your User model doesn't have the id property. You need to make sure that the properties for your model are properly generated.

To narrow down the issue, did you try parsing the id to a temporary variable first before assigning it to your model? E.g.:

var id: Int32 = 0
id <-- json["id"]
createdUser.id = id

maxkonovalov avatar Dec 31 '17 05:12 maxkonovalov

Hi @maxkonovalov I will test your solution. Hope I will have time to come back here. By the way, nice library. Regards.

christophedebatz avatar Jan 02 '18 21:01 christophedebatz

@maxkonovalov @christophedebatz

How is your Int in your JSON file? is it like 123 or "123". If your int is in the string form in the JSON, then Int32 parsing will fail.

We can add this line In the library in the future to support String to Int32 parsing

case is Int32.Type: if let v = Int32(string) { left = v as? T }

Hope this helps :)

s4cha avatar Jan 03 '18 10:01 s4cha