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
How to Deal With @Objc Inference Deprecation With #Selector() in Swift 4
Swiftui: How to Make Textfield Become First Responder
Swift 5.0: 'Withunsafebytes' Is Deprecated: Use 'Withunsafebytes≪R≫(...)
How to Create a Global Variable
How to Declare an Array of Weak References in Swift
How to Change the Textual Representation Displayed For a Type in Swift
Choosing Coredata Entities from Form Picker
Convert String to Url (Why Is Resulting Variable Nil)
What Sorting Algorithm Does Swift Implement For Its Standard Library
Nsattributedstring, Change the Font Overall But Keep All Other Attributes
How to Resolve "Ambiguous Use Of" Compile Error With Swift #Selector Syntax
Append Text or Data to Text File in Swift
Swift Constants: Struct or Enum
Input from the Keyboard in Command Line Application