The use of afnetworking under iOS development--swift

Source: Internet
Author: User

1, first use cocoapods import, if there is no classmate, you can go to see I wrote about cocopods use of the blog

2, directly on the code:

Import Uikitimport afnetworking//enumeration Definition Request Methodenumhttprequesttype{ CaseGET CasePOST}classAfnettools:afhttpsessionmanager {//single-instance class    StaticLet Shareinstance:afnettools ={Let toolinstance=afnettools () toolInstance.responseSerializer.acceptableContentTypes?. Insert"text/html")        returnToolinstance} ()        ///encapsulating get and POST requests///    ///-Parameters:///-RequestType: Request Method///-Urlstring:urlstring///-Parameters: Dictionary parameters///-Completion: CallbackFunc request (Requesttype:httprequesttype, urlstring:string, Parameters: [String:anyobject]?, Completion: @escaping ( Anyobject?) - ()) {                //Successful callbackLet success = {(Task:urlsessiondatatask, Json:any), ()inchCompletion (JSON asAnyobject?)        }                //Failure CallbackLet failure = {(Task:urlsessiondatatask, error:error), ()inchPrint ("Network request error \ (Error)") Completion (nil)}ifRequestType = = . GET {Get(URLString, Parameters:parameters, Progress:nil, Success:success, failure:failure)}Else{post (urlstring, Parameters:parameters, Progress:nil, Success:success, Failure:failure)}} //write successful and failed callbacks separately in two escape closuresFunc request (Requesttype:httprequesttype, url:string, Parameters: [String:any], Succeed: @escaping ([String:any]? ), Failure: @escaping (Error?) ()) {                //Successful closuresLet Successblock = {(Task:urlsessiondatatask, responseobj:any?)inchSucceed (Responseobj as?[String:any])} //failed closuresLet Failureblock = {(task:urlsessiondatatask?, Error:error)inchfailure (Error)}//Get Request        ifRequestType = = . GET {Get(URL, parameters:parameters, Progress:nil, Success:successblock, Failure:failureblock)} //Post Request        ifRequestType = = .    Post {post (URL, parameters:parameters, Progress:nil, Success:successblock, Failure:failureblock)} }    }

The above is the two-time package request class

3, Call:

@IBAction Func Startrequest (_ Sender:any) {Let paramerts= [            "Opt_type":1,            "size": -,            "Offset": -] AFNetTools.shareInstance.request (requesttype:. GET, URL:"http://apiv2.yangkeduo.com/operation/15/groups", Parameters:paramerts, Succeed: {(response)inchGuard Let Dicarray= response? ["goods_list"] as? [[String:any]]Else {                return} print (Dicarray)}) {(error)inchGuard Let Error= ErrorElse {                return} print (Error)}}

Print as follows:

The use of afnetworking under iOS development--swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.