Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Abstract
Alamofire encountered two errors ❌ and a warning ⚠️ when updating to the new version, so make a note of them and how to resolve them. Give other people with similar problems some direction to solve.
A new project was opened today. Since the network request was processed by the third-party library of Alamofire, we introduced Alamofire into the project through Pod and put the packaging code of Alamofire in the last project into this project. The problem came like this.
Module ‘Alamofire’ has no member named ‘request’
This is very frustrating. The code did not have this error in the previous project, but it is reported in the new project. By comparing the version of Alamofire in the two projects, we found that the version in the new project is 5.4.3, which is higher than the version of Alamofire in the previous project.
At this point, the most straightforward solution is to set the Alamofire version in the new project to a lower version than 5.0.0 and re-pod it. Problem solved.
But since the new version of Alamofire doesn’t support this way of using it, let’s call it in a new way and learn.
When searching for request in the new version of the Alamofire code and seeing this method in the Session class, and looking at the Alamofire. Swift file, in addition to indicating the version number, we define a Session constant:
public let AF = Session.default
Copy the code
So, we can replace it with AF.
// Old code alamofire.request (...) // New code af.request (...)Copy the code
This is followed by a warning about setting headers ⚠️, which is not an error, but is invalid if not handled.
Because the headers object I set earlier was using NSMutableDictionary:
let headers = NSMutableDictionary(
dictionary: ["Accept":"application/json",
"Content-Type":"application/json"]
)
Copy the code
HTTPHeaders () {HTTPHeaders () {HTTPHeaders ();}}
private var headers: [HTTPHeader] = []
Copy the code
There is also about initialization, add, delete related methods, interested can catch up to see, here explains the elimination warning ⚠️ processing. Here’s how I handled it:
var headers = HTTPHeaders(
[HTTPHeader.accept("application/json"),
HTTPHeader.contentType("application/json")]
)
Copy the code
If you want to add a new type of HTTPHeader data, you can do this:
headers.add(
HTTPHeader(name: "mw-jwt", value: MyUserDefaults.instance.token)
)
Copy the code
Initialization and addition methods are not the only one, if you are interested, you can take a look at the source code, perhaps have a different perception.
If the responseJSON function is used after the request to convert the result to JSON format, You will find that when response.result data is used the error “isSuccess” is inaccessible due to ‘internal’ protection level, etc.
After investigation, it is found that all attribute variables of Response. result are inaccessible, so there are two attribute variables in Response to replace:
Public var value: Success? {result.success} public var error: Failure? { result.failure }Copy the code
Is to replace isSuccess directly with error == nil.
At this point, two errors ❌ and a warning ⚠️ in the code for updating the old version to the new version of the Alamofire request have been resolved. Let’s see what else happens, and we’ll get back to you.
digression
Time is short and what you said may not be comprehensive. If you encounter any problems during your review, please leave a message in the comment section and I will reply as soon as possible.