Apple released the new Swift language at WWDC in 2014, and 2.0 was officially released not long ago. As an iOS developer, you must keep up with the pace. Therefore, in my spare time recently, I started to rewrite my own projects in Swift and learn new syntax and design patterns. Since I have a relatively short time to learn Swift, today I will briefly talk about how to use the popular AFNetworking request framework in Swift projects. Since there is currently no Swift version of AFNetworking, our project uses cocoapod to install AFNetworking. This is my Prodfile file. The method of using cocoapod is exactly the same as that of using OC. For convenience, we create a class in the project and select the language OC. We will find that there will be an extra SwiftStudy-Bridging-Header.h file in the project. Delete the created OC class and introduce it here. OK, you can use AFNetworking in Swift files
When using OC for project development, a simple external encapsulation of AFNetworking was made. Today we will implement it in Swift. Anyone who has used the new version of AFNetworking knows that it has an AFHTTPSessionManager. Here we inherit AFHTTPSessionManager and use the singleton mode to return an instance of this class. The way to write a singleton in Swift to perform network operations is very different from the OC syntax. I won’t explain Swift syntax here. Please refer to "The Swift Programming Language". Let’s take a look at the code
With the instance of RequestClient, we can encapsulate post, get and upload image files. When using AFNetworking in OC, we are used to using blocks to transfer data, etc. In Swift, we use closures to transfer data This is my file directory SwiftStudy ------------Project Directory AppDelegate.swift myViewController.swift ViewController.swift NetWork ------------Network related RequestClient.swift RequestAPI.swift CustomVC ------------VC encapsulation, CostomNavigationVC.swift BaseViewController.swift Here we mainly use the China Weather Network API to test a PUT request. Let's take a look at RequestAPI.swift.
In ViewController, we get data by calling the GET method of RequestAPI. At the same time, we define two methods, succeed and failed. Pay attention to the parameters in the method and AFHTTPSessionManager. The parameters of the returned data are the same, so the data we get is all the data returned by the server. Calling the GET method of the requestAPI will pass a function pointer, and the data will be returned through the function pointer after the network request is successful. Then we can proceed to other work. If you have used the new version of AFNetworking, you will often encounter errors 3084, 1011, and 1016. These errors are generally data parsing errors. You can change the corresponding data types in AFNetworking, or you can define them yourself according to actual conditions.
In summary, AFNetworking is already the most used network framework. By further encapsulating AFNetworking in a simple outer layer, we can avoid a lot of initialization work when making network requests and directly use a singleton, which improves Efficiency, but also save a lot of code, at least at a glance, it is more comfortable to read. By using Swift to encapsulate AFNetworking, you can also systematically become familiar with the basic syntax, data types, and the use of singletons, closures, etc. of Swift. For Swift beginners, the actual learning effect is quite obvious. With these, you can now use Swift to write a simple joke or news. What are you waiting for? As long as you move forward, you will not fall behind. |
<<: Survey shows that most developers have never built a mobile app
>>: Don’t look down on the three lows, they are the battleground on social networks!
On March 29, Samsung released its long-awaited fl...
I have been working in operations before, so toda...
At present, there are more than 300 charging pile...
Many people believe that the world is about to us...
In the field of operations , a major difference b...
In summer, all the fat people know one thing: &qu...
Expert of this article: Liu Jingjing, PhD in Food...
Those who like to pay attention to the mobile App...
Recently, two new flagships from the Android camp...
I've heard such a little story. Xiao Zhang is...
New knowledge: How to counterattack and play Douy...
"Office Winner: 10 Audio Lectures on High Em...
Brushing teeth and checking Weibo are the first t...