Debacle
Debacle

Reputation: 1221

Swift: Google Translate Format fails

and thanks for checking this question out.

Here's my problem, I'm trying to create a simple Google Translate application. I've already activated it in Google Console so I'm thinking my problem is with my code.

The format returned is

{ "data": { "translations": [ { "translatedText": "bonjour", "detectedSourceLanguage": "en" } ] } }

My code is setup like this:

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)
                }

            }

Sorry for the bad formatting... I'm not sure but I think the problem is on the var jsonResult line how I'm setting the return type to "as Dictionary". The response is a little more complex than that but I'm just not sure how to set it up.

When I run the program it stops on the line mentioned above (jsonResult) and goes to the swift_conformsToProtocol setting

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

0x10c21058a: movaps %xmm0, -0x70(%rbp)

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

Thanks in advance for any help.

Upvotes: 1

Views: 309

Answers (1)

Mundi
Mundi

Reputation: 80265

You cast your top level JSON object as <String:String> while in fact it is <String:AnyObject>, the value of your top level dictionary being another dictionary.

Upvotes: 1

Related Questions