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!
Winter vacation is here Game platforms have relea...
"The graceful crested ibis comes to the spri...
Review expert: Liang Yujun, Associate Professor, ...
Since its birth in 1962, Marshall audio has becom...
[[267204]] This article is authorized to be publi...
Produced by: Science Popularization China Author:...
According to the latest data from AutoForecast So...
Nowadays, marketing is basically online marketing...
When people eat grains, it is inevitable that the...
Especially in recent times, Baidu has been acting...
In a year, it is always necessary to plan and exe...
[email protected] - The status of the app and i...
The public relations departments of various compa...
Online activities must be able to produce short-t...