Діма Комар Діма Комар - 3 months ago 13
JSON Question

How to parse Int with a value more than 2147483647 from JSON in Swift

I have some JSON Dictionary that looks like this:

{
biography = "";
externalUrl = "";
followersCount = 467;
followingsCount = 428;
fullName = dima;
hasAnonymousProfilePicture = 1;
id = 2673580202;
isBusiness = 0;
isPrivate = 0;
mediaCount = 0;
picture = "http://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg";
username = "kmr.dma";
}


So I'm parsing 'id' parameter like this:

guard let userID = array["id"] as? Int else {
return
}


And I'm getting the value like :
-1621387094


Thats because Int.max() is
2147483647
and my id is
2673580202


So the problem is clear but how to get over it ?

Answer

Try to parse it as Double. Double is much bigger than Int.

Example:

guard let userID = array["id"] as? Double else {
    return
}
Comments