json - IOS Debug error (libc++abi.dylib: terminate called throwing an exception (lldb)) -
i developing ios translate application using yandex api. followed tutorial: tutorial
my viewcontroller.m file looks (i took out api key):
#define kbgqueue dispatch_get_global_queue(dispatch_queue_priority_default, 0) //1 #define translatetext [nsurl urlwithstring: @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=apikey&lang=en-es&text=to+be,+or+not+to+be%3f"] //2 #import "viewcontroller.h" @end @interface nsdictionary(jsoncategories) +(nsdictionary*)dictionarywithcontentsofjsonurlstring:(nsstring*)urladdress; -(nsdata*)tojson; @end @implementation nsdictionary(jsoncategories) +(nsdictionary*)dictionarywithcontentsofjsonurlstring:(nsstring*)urladdress { nsdata* data = [nsdata datawithcontentsofurl: [nsurl urlwithstring: urladdress] ]; __autoreleasing nserror* error = nil; id result = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]; if (error != nil) return nil; return result; } -(nsdata*)tojson { nserror* error = nil; id result = [nsjsonserialization datawithjsonobject:self options:kniloptions error:&error]; if (error != nil) return nil; return result; } @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; dispatch_async(kbgqueue, ^{ nsdata* data = [nsdata datawithcontentsofurl: translatetext]; [self performselectoronmainthread:@selector(fetcheddata:) withobject:data waituntildone:yes]; }); } - (void)fetcheddata:(nsdata *)responsedata { //parse out json data nserror* error; nsdictionary* json = [nsjsonserialization jsonobjectwithdata:responsedata //1 options:kniloptions error:&error]; nsarray* translatedtext = [json objectforkey:@"text"]; //2 nslog(@"text translated: %@", translatedtext); //3 // 1) latest loan nsdictionary* ttext = [translatedtext objectatindex:0]; // 3) set label appropriately humanreadble.text = [nsstring stringwithformat:@"latest loan: %@ %@ needs another", [ttext objectforkey:@"name"], [(nsdictionary*)[ttext objectforkey:@"location"] objectforkey:@"country"]]; } @end
when run app (iphone 6.1 simulator), gives me error , app pauses.
2013-07-31 09:57:51.111 translate[76046:c07] text translated: ( "ser, o no ser?" ) 2013-07-31 09:57:51.146 translate[76046:c07] -[__nscfstring objectforkey:]: unrecognized selector sent instance 0x71d1ef0 2013-07-31 09:57:51.150 translate[76046:c07] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[__nscfstring objectforkey:]: unrecognized selector sent instance 0x71d1ef0' *** first throw call stack: (0x1c91012 0x10cee7e 0x1d1c4bd 0x1c80bbc 0x1c8094e 0x2c57 0x10e26b0 0xb0e765 0x1c14f3f 0x1c1496f 0x1c37734 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x12ffc 0x1f5d 0x1e85) libc++abi.dylib: terminate called throwing exception (lldb)
it opens file main.m:
#import <uikit/uikit.h> #import "appdelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return uiapplicationmain(argc, argv, nil, nsstringfromclass([appdelegate class])); } }
it points line:
return uiapplicationmain(argc, argv, nil, nsstringfromclass([appdelegate class]));
and says thread 1: signal sigabrt
what doing wrong?
from nslog output seems that
nsarray* translatedtext = [json objectforkey:@"text"];
is array of strings, not array of dictionaries. should replace
nsdictionary* ttext = [translatedtext objectatindex:0];
by
nsstring* ttext = [translatedtext objectatindex:0];
and replace [ttext objectforkey:@"name"]
ttext
.
Comments
Post a Comment