Swift tutorial 16-use Swift to call AFNetworking for network requests
How can I use Swift for network requests?
First, create a Swift project named SwiftDemo.
Then create a new Oc class in the project, and Xcode will ask you if you want to create a SwiftDemo-Bridging-Header.h file for bridging Oc and Swift
Selecting YES will generate the project name-Bridging-Header.h file, in which we introduce the header file corresponding to the Oc framework to call the Oc code in the Swift project
The current AFNetworking framework is written in Oc. To use AF in Swift, You need to import AFNetworking. h In the Bridging-Header.h file.
The effect is as follows:
Simplest AF Get request
//// AFRequestTest. swift // SwiftDemo /// Created by MBinYang on 15/4/9. // Copyright (c) 2015 cc. huanyouwang. all rights reserved. // import UIKitclass AFRequestTest: NSObject {func afRequestTest () {var paramDict = [access_token: Token, uid: 5117873025] var afManager = AFHTTPRequestOperationManager () var op = afManager. GET (http://lovemyqq.sinaapp.com/getState.php, parameters: ParamDict, success: {(operation: AFHTTPRequestOperation !, ResponseObject: AnyObject !) In var arr: AnyObject! = NSJSONSerialization. JSONObjectWithData (responseObject as NSData, options: NSJSONReadingOptions. AllowFragments, error: nil) // print the array or dictionary element println (arr !) // Avoid the unowned var unSelf: AFRequestTest = self // self. xxx // operation}, failure: {(operation: AFHTTPRequestOperation !, Error: NSError !) In println (request Error: + error. localizedDescription) unowned var unSelf: AFRequestTest = self // self. xxx operation // error related operation}) op. responseSerializer = AFHTTPResponseSerializer () op. start ()}}
Call a network request and call it to test the ViewDidLoad of a ViewController.
override func viewDidLoad() { super.viewDidLoad() AFRequestTest().afRequestTest() }
Output:
{ result = ( { state = { address = U4e2dU56fdU5317U4eacU5e02U671dU9633U533aU5efaU5916U8857U9053U5efaU56fdU8def100U53f7; content = U4f60U662f; date = 2015-01-07 19:06:13; fromUid = 5117873025; id = 6664; image = http://xuyingtest-xuyingttt.stor.sinaapp.com/1420628745userImage.png; imagecount = 1; latitude = 39.907007; longitude = 116.470241; }; }, { state = { address = U4e2dU56fdU5317U4eacU5e02U77f3U666fU5c71U533aU53e4U57ceU8857U9053; content = U516cU79efU91d1; date = 2014-11-23 11:10:57; fromUid = 5117873025; id = 5593; image = http://xuyingtest-xuyingttt.stor.sinaapp.com/1416712253userImage.png; imagecount = 1; latitude = 39.897711; longitude = 116.184322; }; }, { state = { address = ; content = Chen; date = 2014-11-21 11:02:19; fromUid = 5117873025; id = 5551; image = http://xuyingtest-xuyingttt.stor.sinaapp.com/1416538936userImage.png; imagecount = 1; latitude = ; longitude = ; }; } ); success = 0;}
Notes and Instructions:
1. Swift and Oc call methods are in different forms. Oc is a space and Swift is a point number.
2. The closure of Swift network requests is not the same as that of Oc. For details, see
Swift tutorial 15-closure block Multiple Application Methods
Swift tutorial 14-func function, Function Type _ comparison Oc
3. Avoid statements with a strong reference Loop
unowned var unSelf:AFRequestTest = self
No primary reference, no reference count is added; cannot be nil
Or
weak var self2:AFRequestTest! = self
Weak references do not increase the reference count. However, the returned data type is optional and can be nil.
4. type conversion and downward Transformation
ResponseObject as NSData
For downward type conversion, use as or? ;
What is the difference? If the conversion fails, nil is returned. If the conversion succeeds, an optional type is returned.
5. the constructor is similar to Java/C ++.
AFRequestTest().afRequestTest()
The Swift constructor is equivalent to the combination of alloc init of Oc and is used for initialization.
However, the Swift constructor uses the class name in other languages of init ().
The Swift constructor and other object-oriented articles will be logged on later
6. Do not use JSON parsing of AF
op.responseSerializer = AFHTTPResponseSerializer()
AFNetwoking JSON parsing may fail to parse some interfaces, resulting in incorrect output.
Error: Request failed: unacceptable content-type: text/html
At this time, we only need to add the following code to prevent AF from parsing for us. Instead, we can use the built-in JSON parsing of the system.
7. the Post request is similar. The above Code provides a test interface.