2016-09-29 10 views
0

サーバへのリクエストのためのメソッドをBackgroundとMainのキューに入れようとしています。最後に、このメソッドはサーバーから私の応答のNSDictionaryを返す必要があります。サーバへのリクエストでvoid function内に予期しない非void戻り値があります。

func sendRequest(UrlString urlString:String,MethodString method:String)->(NSDictionary) { 

    DispatchQueue.global(qos: .background).async { 
     var request = URLRequest(url: URL(string:urlString)!) 
     request.httpMethod = method 
     //let postString = "id=13&name=Jack" 
     //request.httpBody = postString.data(using: .utf8) 
     let task = URLSession.shared.dataTask(with: request) { data, response, error in 
      guard let data = data, error == nil else {             // check for fundamental networking error 
       print("error=\(error)") 
       return 
      } 

      if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
       print("statusCode should be 200, but is \(httpStatus.statusCode)") 
       print("response = \(response)") 
      } 

      let responseString = String(data: data, encoding: .utf8) 
      print("responseString = \(responseString)") 

     DispatchQueue.main.async { 

      do { 
       let jsonDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject] as NSDictionary 

       return jsonDictionary 

      } catch { 
       //Handle the error 
      } 

      //return airports 
     } 

    } 

    task.resume() 
} 

そして今、試してみるがメインキューにjsonDictionaryを返すために、それは私に、このエラーを示しています

Unexpected non-void return value in void function

そして今、私はこの問題を解決する方法がわかりません。
私はXcode 8,iOS8およびSwift 3を使用しています。
私を助けてください。
ありがとうございます。

答えて

4

少なくとも2つの非同期操作を実装しており、値を返すことができません。非同期操作の場合は、次のような完了ハンドラを使用する必要があります。

func sendRequest(urlString: String, method: String, completion: @escaping (_ dictionary: NSDictionary?, _ error: Error?) -> Void) { 

    DispatchQueue.global(qos: .background).async { 
     var request = URLRequest(url: URL(string:urlString)!) 
     request.httpMethod = method 
     let task = URLSession.shared.dataTask(with: request) { data, response, error in 
      guard let data = data, error == nil else {             // check for fundamental networking error 
       print("error=\(error)") 
       completion(nil, error) 
       return 
      } 

      if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
       print("statusCode should be 200, but is \(httpStatus.statusCode)") 
       print("response = \(response)") 
       // make error here and then 
       completion(nil, error) 
       return 
      } 

      let responseString = String(data: data, encoding: .utf8) 
      print("responseString = \(responseString)") 

      DispatchQueue.main.async { 
       do { 
        let jsonDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] as NSDictionary 

        completion(jsonDictionary, nil) 

       } catch { 
        completion(nil, error) 
       } 
      } 

     } 

     task.resume() 
    } 
} 
+0

この機能を使用すると、どのように呼び出すことができますか?私はurlStringとメソッドを理解することができますので、私は考えていません。例と説明に感謝します。 – LiTHiUM2525

+1

'sendRequest(urlString:" http:// localhost "、メソッド:" GET "){辞書、エラー }' –

+0

私はそれを呼び出してください:sendRequest(urlString : "https://localhost/signup.php"、メソッド: "POST"){dictionary、error in}。データは正しく私のPHPに送信されます。返信がiOSに返信されると、 応答文字列: "{\\\"例外\\\ ":false、\\\" success \\\ ":false、\\\" status \\\ ": - 9 \\\ "message \\\":\\\ "このユーザー名はすでに存在しています!\\\"} \ "")キャッチエラー!なぜjsonDictionaryに行かないのですか? – LiTHiUM2525

関連する問題