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!
Source: Dr. Curious This article has been authori...
01. Books on learning Internet thinking 02.2020 &...
Let me give you the answer first: It is recommend...
China is a country with a large population. In re...
360 Media Promotion is also a search engine adver...
After years of battles among all parties in the f...
In the era of mobile Internet, traffic costs are ...
On August 13, the State Council announced a new b...
Author: Kong Lingfeng Reviewer: Chen Shengjun Whe...
Source code introduction The demo can provide a v...
In 2015, China's Tusi sites were included in ...
gossip In the dry autumn and winter, many familie...
Whether it is out of concern about the taste and ...
In the field of e-commerce, Pinduoduo may be the ...
What is creativity? Creativity refers to the prom...