Attach First:
is still very simple, the main use of storyboard, but ready to learn the next Cocoui to layout, storyboard very inverse days! The other is the HTTP request this piece, also has the NSString class to use!
Attach the main source code:
nsstring* Queryword =Word.text; NSString* Urlstr = [[NSString alloc] Initwithformat:@"http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=5khzheo8mn7l6nmptgv6posb&q=%s& From=en&to=zh", Queryword. Utf8string]; Nsurl* URL =[Nsurl Urlwithstring:urlstr]; Nsurlrequest* request = [[Nsurlrequest alloc] Initwithurl:url cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval: -]; Nshttpurlresponse* Response =Nil; NSData* Returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil]; NSString* Resultdata =[[NSString alloc] init]; Nserror*error; Nsdictionary* JSON = [nsjsonserialization jsonobjectwithdata:returndata options:kniloptions error:&ERROR]; Nsdictionary* data = [JSON Objectforkey:@"Data"]; Nsarray* symbols = [data objectforkey:@"symbols"]; for(intI=0; i<symbols.count;i++) {nsdictionary* Item =[Symbols objectatindex:i]; Nsarray* TEMP = [item Objectforkey:@"Parts"]; for(intK =0; k< temp.count;k++) {nsdictionary* Subitem =[temp objectatindex:k]; Resultdata= [Resultdata stringbyappendingstring:[subitem objectforkey:@" Part"]]; Nsarray* arr = [Subitem Objectforkey:@"means"]; for(intj=0; j<arr.count;j++) {Resultdata=[Resultdata Stringbyappendingstring:[arr objectatindex:j]; } }} result.text= Resultdata;
I can't remember this is the first few dictionary gadgets! Ready to write a weather query function
Learn iOS development series, using the Baidu Dictionary API written by the iOS client program