NSMutableString *emptyStr = [[NSMutableString alloc] initWithString:@"\n "];
NSString *baseStr = @"
http://fanyi.youdao.com/openapi.do?keyfrom=LoveDict&key=1783061912&type=data&doctype=json&version=1.1&q=";
NSString *appenedStr = [baseStr stringByAppendingString:_searchText.stringValue];
NSURL *url = [NSURL URLWithString:appenedStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *translationArray = [self.dic objectForKey:@"translation"];
NSString *translation = [translationArray objectAtIndex:0];
NSDictionary *basicDic = [self.dic objectForKey:@"basic"];
NSString *ukphonetic = [basicDic objectForKey:@"uk-phonetic"];
NSString *usphonetic = [basicDic objectForKey:@"us-phonetic"];
NSArray *explainsArray = [basicDic objectForKey:@"explains"];
for (NSString *eachExplain in explainsArray){
[emptyStr appendString:eachExplain];
[emptyStr appendString:@"\n "];
}
_resultText.stringValue = [NSString stringWithFormat:@" 翻译: %@\n 英式发音: [%@]\n 美式发音: [%@]\n 释义: %@",
translation, ukphonetic, usphonetic, emptyStr];
}] resume];