Swift+GoogleTranslateAPIを使って翻訳アプリを作っているのですが、URLを叩く際に、スペースが入るとエラーがでます。
lang
1 // 通信用のConfigを生成. 2 let myConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() 3 4 // Sessionを生成. 5let mySession:NSURLSession = NSURLSession(configuration: myConfig, delegate: self, delegateQueue: nil) 6 7 // 通信先のURLを生成. 8let myUrl:NSURL = NSURL(string: "https://www.googleapis.com/language/translate/v2?key=/*ここにキーが入ってます。*/=\(myTextView.text)")! 9 10 // タスクの生成. 11 let myTask:NSURLSessionDataTask = mySession.dataTaskWithURL(myUrl, completionHandler: { (data, response, err) -> Void in 12 13 // 受け取ったjsonデータを表示. 14dispatch_async(dispatch_get_main_queue(), { 15 self.myTextViewResult.text = NSString(data: data, encoding: NSUTF8StringEncoding)! as String }) 16 17 // 受け取ったJSONデータをパースする. 18 var json:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSDictionary 19 20
"He"や"Hello"など単語や文字をつなげてmyTextViewに入力するとエラーがでずに翻訳されたものが返ってくるのですが、"Hello guys!"や"Good Morning"などスペースをあけると
//通信先のURLを生成
のところでエラーがでてしまいます。
どのように処理すればよいのでしょうか・・・どなたか教えていただけないでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。