如何使用Swift 3在Alamofire上使用singleton?

kis*_*011 1 singleton http ios swift alamofire

我是iOS的新手,我对如何使用Alamofire的单身以及单身如何重要感到困惑.我创建了一个networkWrapper类,因为我编写了Alamofire post和get方法,但我没有使用singleton.

如何用单例创建一个包装类的Alamofire?我怎样才能获得真正重要的所有技巧?

下面是我的包装类代码:

import Foundation
import UIKit
import Alamofire
import SwiftyJSON

class AFWrapper: NSObject {

//TODO :-
/* Handle Time out request alamofire */


 class func requestGETURL(_ strURL: String, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void)
    {
        Alamofire.request(strURL).responseJSON { (responseObject) -> Void in
            //print(responseObject)
            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                //let title = resJson["title"].string
                //print(title!)
                success(resJson)
            }

        if responseObject.result.isFailure {
            let error : Error = responseObject.result.error!
            failure(error)
        }
    }
  }

static func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
    Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
        //print(responseObject)
        if responseObject.result.isSuccess {
            let resJson = JSON(responseObject.result.value!)
            success(resJson)
        }
        if responseObject.result.isFailure {
            let error : Error = responseObject.result.error!
            failure(error)
        }
    }
  }
}  
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

           if newLength == 6
            {
                let textZipCode = textField.text! + string

                let dict = ["id" : "43","token": "2y103pfjNHbDewLl9OaAivWhvMUp4cWRXIpa399","zipcode" : textZipCode] as [String : Any]

                //Call Service
               AFWrapper.requestPOSTURL(HttpsUrl.Address, params: dict as [String : AnyObject]?, headers: nil, success: { (json) in
                    // success code
                    print(json)
                }, failure: { (error) in
                    //error code
                    print(error)
                })


                setFields(city: "Ajmer", state: "Rajasthan", country: "India")
                return newLength <= 6
            }
Run Code Online (Sandbox Code Playgroud)

Vin*_*gan 6

我没有仔细研究你的代码.在swift中我们可以创建单例

static let sharedInstance = AFWrapper()
Run Code Online (Sandbox Code Playgroud)

它将创建一个类的单例实例,因此不需要单例类实例函数的类和静态.请参考下面的单身类代码.

import Foundation
import UIKit
import Alamofire
import SwiftyJSON

class AFWrapper: NSObject {

    static let sharedInstance = AFWrapper()

    //TODO :-
    /* Handle Time out request alamofire */


    func requestGETURL(_ strURL: String, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void)
    {
        Alamofire.request(strURL).responseJSON { (responseObject) -> Void in
            //print(responseObject)
            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                //let title = resJson["title"].string
                //print(title!)
                success(resJson)
            }

            if responseObject.result.isFailure {
                let error : Error = responseObject.result.error!
                failure(error)
            }
        }
    }

    func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){
        Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
            //print(responseObject)
            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                success(resJson)
            }
            if responseObject.result.isFailure {
                let error : Error = responseObject.result.error!
                failure(error)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以通过调用Singleton类实例函数

AFWrapper.sharedInstance.requestPOSTURL(HttpsUrl.Address, params: dict as [String : AnyObject]?, headers: nil, success: { (json) in
    // success code
    print(json)
}, failure: { (error) in
    //error code
    print(error)
})
Run Code Online (Sandbox Code Playgroud)