class ApiManager {
let url = "https://15f1-61-246-140-18.in.ngrok.io/Legal251AppAPI_IOS/api/lgapp/index.php?parameter=sendotp"
static let sharedInstance = ApiManager()
func callingApi(signUp : myModel){
let headers: HTTPHeaders = [
"Authkey": Secrets.AuthKey
]
AF.request(url, method: .post, parameters:signUp , encoder: JSONParameterEncoder.default, headers: headers).response{
response in debugPrint(response)
}
}
}
我的回復
{
"code": 200,
"status": "success",
"message": "SMS IS SENT",
"data": [
{
"status": 1,
"number": "9999911144"
}
]
}
我的成功回應來自資料陣列
我試圖將網路類設為通用但我無法為在 api 中發布正文創建通用引數
這是我的注冊模型,我的 api 主體
struct myModel: Encodable{
var number : String = String()
var action :String = String()
}
我需要實作我探索了很多但沒有達到標準答案的網路通用類
我希望在這里我會得到
先感謝您
uj5u.com熱心網友回復:
該 APIresponseJSON已棄用,僅response回傳Data.
然而,最推薦的 API 是responseDecodable將 JSON 直接解碼為模型。
您可以使您的 API 呼叫通用
class ApiManager {
let url = "https://15f1-61-246-140-18.in.ngrok.io/Legal251AppAPI_IOS/api/lgapp/index.php?parameter=sendotp"
static let sharedInstance = ApiManager()
func callingApi<Input: Encodable, Output: Decodable>(signUp : Input, type: Output.Type){
let headers: HTTPHeaders = [
"Authkey": Secrets.AuthKey
]
AF.request(url, method: .post,
parameters: signUp,
encoder: JSONParameterEncoder.default,
headers: headers).responseDecodable(of: Output.self, decoder: JSONDecoder()) { response in
switch response.result {
case .success(let result): print(result)
case .failure(let error): print(error)
}
}
}
}
在type引數中傳遞根物件的型別,例如Response.self
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/468555.html
下一篇:Swift:從JSON轉換為
