Learning the ios development series, using the ios dictionary app and iosapp written by Baidu dictionary api
Attached first:
It is still very simple. storyboard is mainly used, but it is ready to be learned to layout cocoui. storyboard is very backward! The other is the http request, and the NSString class is used!
The main source code is attached:
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:30]; 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(int i=0;i<symbols.count;i++) { NSDictionary* item = [symbols objectAtIndex:i]; NSArray* temp = [item objectForKey:@"parts"]; for (int k = 0; k< temp.count;k++) { NSDictionary* subItem = [temp objectAtIndex:k]; resultData = [resultData stringByAppendingString:[subItem objectForKey:@"part"]]; NSArray* arr = [subItem objectForKey:@"means"]; for(int j=0;j<arr.count;j++) { resultData = [resultData stringByAppendingString:[arr objectAtIndex:j]]; } } } result.text = resultData;
I cannot remember the first few dictionary gadgets! Prepare a weather query function.