iOS 开发实用技术导航
NSHipster 中文版
› http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
› http://www.cocos2d-iphone.org/
CocoaPods
› http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
› http://code.google.com/mobile/analytics/
WWDC
› https://developer.apple.com/wwdc/
Design Guides and Resources
› https://developer.apple.com/design/
Transcripts of WWDC sessions
› http://asciiwwdc.com
Cocoa with Love
› http://cocoawithlove.com/
Cocoa Dev Central
› http://cocoadevcentral.com/
NSHipster
› http://nshipster.com/
Style Guides
› Google Objective-C Style Guide
› NYTimes Objective-C Style Guide
Useful Tools and Services
› Charles Web Debugging Proxy
› Smore
henryshen233
V2EX  ›  iDev

为什么我调用有道 API 非常慢

  •  
  •   henryshen233 · Mar 4, 2017 · 3843 views
    This topic created in 3493 days ago, the information mentioned may be changed or developed.
    买的同样调用有道 API 的字典 Dictionary for iOS 就非常快,我是在 macOS App 开发上调用,却非常慢,常常要五秒以上才会返回结果,我用的是 NSJSONSerialization 来解析 JSON ,请问是否有什么问题,谢谢大家了
    1 replies  •  2017-03-04 21:34:15 +08:00
    henryshen233
        1
    henryshen233  
    OP
       Mar 4, 2017
    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];
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2717 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:11 · PVG 15:11 · LAX 00:11 · JFK 03:11
    ♥ Do have faith in what you're doing.