我有帖子請求和解析響應json代碼,但我的代碼不工作。Swift 3請求帖子,回覆返回無
我的類代碼
@discardableResult
open func getLoginMethod(_ method: String, parameters: String, completionHandler: @escaping (_ login: [Login]?, _ error: Error?) -> Void) -> URLSessionTask! {
let session = URLSession.shared
guard let url = NSURL(string: parameters) else {
completionHandler(nil, myErrors.InvalidUrlError)
return nil
}
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
let sendingdata = "Simple=1"
request.httpBody = sendingdata.data(using: String.Encoding.utf8);
let task = session.dataTask(with: request as URLRequest) { data, response, error in
if error != nil {
completionHandler(nil, myErrors.getError)
} else {
do {
let login = try self.getLogin(jsonData: data! as NSData)
print(login)
completionHandler(login, nil)
} catch {
completionHandler(nil, error)
}
}
}
task.resume()
return task
}
當我點擊動作按鈕的代碼給我只能
零
輸出。我在哪裏做錯了任何想法?
@Vinodh耶確保模型的代碼實現。我認爲request.httpBody = sendingdata.data錯誤(使用:String.Encoding.utf8);或如果讓jsonArray =嘗試JSONSerialization.jsonObject(與:jsonData作爲數據,選項:.allowFragments)爲? [字符串:AnyObject] { – SwiftDeveloper
@Vinodh我也加入了登錄模式 – SwiftDeveloper
@Vinodh nope仍然相同 – SwiftDeveloper