2016-09-26 55 views
0

我有一個在Swift 3遷移之前工作的移動SDK。Swift 3 - 錯誤 - 無法使用類型爲'(使用:String.Encoding)'的參數列表調用'data''

我得到以下錯誤:

不能調用與類型的參數列表 '數據' '(使用:String.Encoding)'

這裏:

open class func applyTheCode(
     _ theCode: String, 
     forTenant tenant: String, 
     toUserID userID: String, 
     toAccountID accountID: String, 
     withToken token: String, 
     completionHandler: @escaping (_ userInfo: AnyObject?, _ error: NSError?) -> Void) { 

      let url = baseURL.appendingPathComponent("path/to/api/call") 
      let request = NSMutableURLRequest(url: url) 
      request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
      request.setValue(token, forHTTPHeaderField: "token") 
      request.httpMethod = "POST" 
      request.httpBody = NSString(string: "{}").data(using: String.Encoding.utf8) 

      let dataTask = companyDataTaskStatusOKWithRequest(request as URLRequest, withCallback: completionHandler) 
      dataTask.resume() 
} 

有問題的線路:

request.httpBody = NSString(string: "{}").data(using: String.Encoding.utf8) 

我的問題是有沒有更好的寫這行代碼的方法,或者有人遇到這個問題,併成功地將它遷移到swift 3語法。


事情我已經嘗試:

request.httpBody = String("{}").data(using: String.Encoding.utf8) 

但我不知道它是相同的,但我可能是錯誤的。


任何方向或鏈接,或解答歡迎:d

+0

。 :.utf8)'? – zneak

+0

'request.httpBody =「{}」。data(using:.utf8)' 工作時沒有拋出錯誤。現在看看它是否真的有效:P謝謝。 – Dominique

回答

2

您正在使用的NSString,你應該做的怎麼樣` 「{}」 的數據(使用的是使用字符串

request.httpBody = "{}".data(using: String.Encoding.utf8) 
相關問題