学习ios开发系列之,利用百度词典api写的ios客户端程序
先附上效果图:
还是很简单,主要用的storyboard,不过准备学习下cocoui来布局了,storyboard很逆天! 其他的就是http请求这块,还有NSString类使用了!
附上主要的源代码:
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;
已经想不起来这是第几个词典小工具了!再准备写一个天气查询的功能
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。