【问题标题】:Swift: Google Translate Format fails斯威夫特:谷歌翻译格式失败
【发布时间】:2014-09-07 16:18:49
【问题描述】:

感谢您检查这个问题。

这是我的问题,我正在尝试创建一个简单的 Google 翻译应用程序。我已经在 Google Console 中激活了它,所以我认为我的问题出在我的代码上。

返回的格式是

{ "数据": { "翻译": [ { "translatedText": "你好", "detectedSourceLanguage": "en" } ] } }

我的代码是这样设置的:

var session = NSURLSession.sharedSession()

        var task = session.dataTaskWithURL(transURL){
            data, response, error -> Void in
            NSLog("dkc 1")
            if(error != nil){
                println(error.localizedDescription)
            }

            var jsonError : NSError?
            NSLog("dkc 2")
            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary<String, String>
            NSLog("dkc 2.5"
            if (jsonError? != nil){
                println(jsonError!.localizedDescription)
            }
            NSLog("dkc 3")
            if let apiDelegate = self.delegate?{

                dispatch_async(dispatch_get_main_queue()){
                    NSLog("dkc 4")
                    apiDelegate.didFinishGTTranslation(jsonResult)
                }

            }

抱歉格式错误...我不确定,但我认为问题出在 var jsonResult 行上,我如何将返回类型设置为“作为字典”。响应比这要复杂一些,但我只是不确定如何设置它。

当我运行程序时,它会停在上面提到的行 (jsonResult) 并转到 swift_conformsToProtocol 设置

0x10c210572: nopw %cs:(%rax,%rax) 0x10c210580: movq %rax, -0x108(%rbp) 0x10c210587: xorps %xmm0, %xmm0

0x10c21058a: 移动 %xmm0, -0x70(%rbp)

0x10c21058e: movq $0x0, -0x60(%rbp) 0x10c210596: movq (%rdx), %rax 0x10c210599: cmpq $0x41, %rax

提前感谢您的帮助。

【问题讨论】:

  • 我正在尝试在我的项目中使用您的代码,您将什么设置为 transURL?

标签: ios json swift google-translate


【解决方案1】:

您将顶级 JSON 对象转换为 &lt;String:String&gt;,而实际上它是 &lt;String:AnyObject&gt;,顶级字典的值是另一个字典。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-03-24
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多