I'm busy working on an iPad application and my web service returns pretty simple JSON data. All seems well and I have other methods doing t开发者_如何学JAVAhis same conversion without issue however, I have 1 method that returns a random string when doing a integer -> string conversion.
My userdata object below is a NSDictionary created by the SBJSON parser. The value when debugging of [userdata objectForKey:@"UserID"] is 1.
However when I do this
NSString *userId = [NSString stringWithFormat:@"%d", [userdata objectForKey:@"UserID"]];
The value in userId is or appears to be a random number such as 23425234. I also tried the %d in my format but got the same result.
Because it is an object, not an int, you see the address of the object, instead, you can do that:
NSString *userId = [NSString stringWithFormat:@"%@", [userdata objectForKey:@"UserID"]];
^
Try with:
NSString *userId = [NSString stringWithFormat:@"%d", [[userdata objectForKey:@"UserID"] intValue]];
Or:
NSString *userId = [NSString stringWithFormat:@"%@", [userdata objectForKey:@"UserID"]];
object for key is probably NSNumber and not int...
精彩评论