2016-09-16 30 views
2

我是新來的swift編程,需要你的一點幫助原諒我,如果我問一些愚蠢的東西。我正在嘗試從我的UIViewController調用一個函數來發送一個POST請求到API。調用函數是這樣表達式解析爲一個未使用的函數(Swift)

@IBAction func actionStartSignIn(sender: AnyObject) { 
      let email: String = txtEmail.text! 
      let password: String = txtPassword.text! 
      if !email.isEmpty && !password.isEmpty && General.isValidEmail(email) && password.characters.count>6{ 
       var request = RequestResponse() 
       request.email = email 
       request.password = password 
       let isValid = NSJSONSerialization.isValidJSONObject(request) 
       print(isValid) 
       var requestBody: String = "" 

       // Facing issue in following line 
       RequestManager.sharedInstance.postRequest(Constants.BASE_URL + Constants.LOGIN, body: requestBody, onCompletion: {(json: JSON) in{ 
          let result = json["response"] 
          print(result) 

         } 
        } 
       ) 
      } 
     } 

叫功能是這樣

func postRequest(route: String, body: String, onCompletion: (JSON) -> Void) { 
     makeHTTPPostRequest(route, requestBody: body, onCompletion: { json, err in 
      onCompletion(json as JSON) 
     }) 
    } 

此外,

// MARK: Perform a POST Request 
    private func makeHTTPPostRequest(path: String, requestBody: String, onCompletion: ServiceResponse) { 
     let request = NSMutableURLRequest(URL: NSURL(string: path)!) 

     // Set the method to POST 
     request.HTTPMethod = "POST" 

     do { 
      // Set the POST body for the request 
      // let jsonBody = try NSJSONSerialization.dataWithJSONObject(body, options: .PrettyPrinted) 
      // request.HTTPBody = jsonBody 
      request.HTTPBody = requestBody.dataUsingEncoding(NSUTF8StringEncoding); 
      let session = NSURLSession.sharedSession() 

      let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
       if let jsonData = data { 
        let json:JSON = JSON(data: jsonData) 
        onCompletion(json, nil) 
       } else { 
        onCompletion(nil, error) 
       } 
      }) 
      task.resume() 
     }/* catch { 
      // error 
      onCompletion(nil, nil) 
     }*/ 
    } 

typealias ServiceResponse = (JSON, NSError?) -> Void 

我面對「表達式解析爲一個未使用的功能「,而cal ling

RequestManager.sharedInstance.postRequest(Constants.BA‌​SE_URL + Constants.LOGIN, body: requestBody, onCompletion: {(json: JSON) in{ let result = json["response"] print(result) } }) 

可能是我缺少一些基本的語法。任何幫助將非常感激。

謝謝。

+0

哪裏是你的問題?你遇到了哪個問題? – t4nhpt

+0

表達式解析爲一個未使用的函數。 雖然調用 RequestManager.sharedInstance.postRequest(Constants.BASE_URL + Constants.LOGIN, body: requestBody, onCompletion: {(json: JSON) in{ let result = json["response"] print(result) } } )

+0

請更新它到您的問題。 – t4nhpt

回答

3

刪除{ }階段後in將解決問題。 它應該是這樣的:

​​3210

爲封PARAM,你不應該鍵入它自己,以防止輸入錯誤。使用tab key選擇該參數,然後按enter,xCode會自動爲您生成代碼。

如果用我剛纔說的方式,尾隨關閉將是這樣的:

RequestManager.sharedInstance.postRequest(Constants.BASE_URL + Constants.LOGIN, body: requestBody) { (json) in 
         let result = json["response"] 
         print(result) 
       } 
+0

ohh ..謝謝。 我的猜測是「語法」錯誤.. :( 對不起我從JAVA後臺面對斯威夫特瓶蓋困難的時候。 感謝您的時間。:) –

+0

@ t4nhpt偉大的答案!非常感謝你!節省很多時間。 –