开发者

Objective-c integer to string giving random numbers?

开发者 https://www.devze.com 2023-03-24 02:40 出处:网络
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'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...

0

精彩评论

暂无评论...
验证码 换一张
取 消