gpt4 book ai didi

ios - 如何使用 Swift 3 将单例与 Alamofire 一起使用?

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:47 24 4
gpt4 key购买 nike

我是 iOS 的新手,我对如何将单例与 Alamofire 一起使用以及单例的重要性感到有些困惑。我创建了一个 networkWrapper 类,因为我已经编写了 Alamofire post 和 get 方法,但我没有使用单例。

如何使用单例创建 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)
}
}
}
}

在我的 Controller 中:

           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
}

最佳答案

我没有深入研究您的代码。在 swift 中,我们可以通过以下方式创建单例

static let sharedInstance = AFWrapper()

并且它会创建一个类的单例实例,这样单例类实例函数的class和static就不是必须的了。请引用下面的单例类代码。

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)
}
}
}
}

现在可以调用单例类实例函数了

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)
})

关于ios - 如何使用 Swift 3 将单例与 Alamofire 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com