Reputation: 1304
I'm having issue parsing a dictionary object returned in a json message from a webservice. The json is valid as AFNetworking is parsing it successfully. The actual response is below:
{
error = FALSE;
"error_desc" = "";
images = (
{
hasItem = 0;
image = "https://image/php4A6Xb8";
imageGroupId = 28;
"image_date" = "06/07/2014";
"image_id" = 863;
tag = "MCMOBILE-06072014-033902";
thumb = "https://image/thumbs/php4A6Xb8";
}
);
}
With this response my code does the following:
- (void) successResponseImages: (NSDictionary *) dictionary {
NSLog(@"Success: %@", dictionary);
NSString *error = [ dictionary objectForKey:@"error"];
NSString *errorDesc = [ dictionary objectForKey:@"error_desc"];
NSArray *images = [ dictionary objectForKey:@"images"];
....
At this point all values are correct and images is an array of 7 items.
i then try to loop through images using:
for (NSDictionary * image in images) {
NSLog([image objectForKey:@"image_id"]); <<<<fail
NSLog([image objectForKey:@"image"]);
NSLog([image objectForKey:@"thumb"]);
}
At fail it bombs out with the following error:
2014-07-06 15:44:19.161 mycobber[8976:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x9ad2690
I'm not sure what the problem is.
EDIT: fuller stack trace
2014-07-06 15:44:19.292 mycobber[8976:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x9ad2690'
*** First throw call stack:
(
0 CoreFoundation 0x0214f1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01ece8e5 objc_exception_throw + 44
2 CoreFoundation 0x021ec243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0213f50b ___forwarding___ + 1019
4 CoreFoundation 0x0213f0ee _CF_forwarding_prep_0 + 14
5 CoreFoundation 0x020cf89c CFStringGetLength + 140
Upvotes: 0
Views: 1182
Reputation: 1304
cracked it:
NSLog([NSString stringWithFormat:@"%@",[image objectForKey:@"image_id"]]);
Upvotes: 0
Reputation: 237010
The first argument to NSLog is a format string, but your dictionary has a number set for its @"image_id"
key. NSLog tries to treat this as a string by asking for its length
, but this doesn't work because NSNumber has no such method. The format string to print a single object is @"%@"
.
Upvotes: 1
Reputation: 14995
Your code looks correct only. But your nslog statement is not correct and also no need to write stringwithformat as well just simply write like that below, it works -
NSLog(@"%@",[image objectForKey:@"image_id"]);
Upvotes: 0