Alamofire With -D

Alamofire with Swift Combine

Yes, you need to provide the compiler the type you want as a result. You can either do this by adding a parameter to take the type, like Alamofire does (type: T.self, which you can default using T.Type = T.self) or you can capture the publisher and provide the type.

let publisher: AnyPublisher<SomeType, Error> = performCombineRequest(...)

I suggest passing the type parameter.

Using Alamofire with .publishDecodable

You should help the compiler to infer the T type be providing the type of the response that you expect. For example your call to request(_:method:headers:parameters:) function might look like this:

cancellation = request(serverURL + "login", method: .post, parameters: parameters)
.sink { [self] (response: Result<[LoginModel], AFError>) in
switch response {
case .success(let value):
self.loginModel = value
case .failure(let error):
print(error)
}
}

Or if you prefer you can specify the return type of the request(_:method:headers:parameters:) function like this:

let publisher: AnyPublisher<Result<[LoginModel], AFError>, Never>
publisher = request(serverURL + "login", method: .post, parameters: parameters)
cancellation = publisher.sink { [self] response in
switch response {
case .success(let value):
self.loginModel = value
case .failure(let error):
print(error)
}
}

How to send request in Alamofire with parameters and headers using POST method in swift

Alamofire 4.0

let headers = ["Content-Type":"Application/json"]


Alamofire.request(requestString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
print("Request \(response.request)")

print("RESPONSE \(response.result.value)")
print("RESPONSE \(response.result)")
print("RESPONSE \(response)")


switch response.result {
case .success:


case .failure(let error):


}
}

in 3.0 u can also add headers like this . In parameters to func



Related Topics



Leave a reply



Submit