【问题标题】:Swift Alamofire SwiftyJSON Asynchronous/Synchronous Class MethodsSwift Alamofire SwiftyJSON 异步/同步类方法
【发布时间】:2015-07-23 17:55:31
【问题描述】:

所以我目前有以下:

class ViewController: UIViewController {

class Identity{
    let baseUrl = "superSecretURL"
    var _username: String = ""
    var _password: String = ""
    var _apiKey: String = ""

    init(){

    }

    init(username: String, apiKey: String){
        _username = username
        _apiKey = apiKey
    }

    init(username: String, password: String){
        _username = username
        _password = password
    }

    func loginPassword() -> String{
        var loginJSON = ["auth": ["passwordCredentials": ["username": _username, "password": _password]]];
        var returnJSON: String

        request(.POST, baseUrl, parameters: loginJSON, encoding: .JSON)
            .responseJSON { (request, response, data, error) in
                if let anError = error
                {
                    // got an error in getting the data, need to handle it
                    println("error calling POST on /posts")
                    println(error)
                }
                else if let data: AnyObject = data
                {
                    // handle the results as JSON, without a bunch of nested if loops
                    let post = JSON(data)
                    // to make sure it posted, print the results
                    println("JSON Returned")
                }
        }
    }
}

var i = Identity(username: "secretName", password: "complicatedPassword")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    println("Before Call")



    println("After Call")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

基本上我希望能够调用 println("Before Call"),然后从 loginPassword() 方法接收响应,然后 println("After Call")。我相信这是同步的,但我想不出一种让它工作的方法,整个线程的事情让我感到困惑。

我基本上想说:

if i.loginPassword(){ // do some login stuff }else{ // do some error stuff }

感谢任何帮助或指点。

【问题讨论】:

    标签: swift asynchronous synchronous alamofire swifty-json


    【解决方案1】:

    您需要在loginPassword() 函数中设置一个随时调用的回调函数。

    这可能是实现它的一种方式:

    func loginPassword(callback: ((isOk: Bool)->Void)?) -> String{
     var loginJSON = ["auth": ["passwordCredentials": ["username": _username, "password": _password]]];
     var returnJSON: String
    
     request(.POST, baseUrl, parameters: loginJSON, encoding: .JSON)
       .responseJSON { (request, response, data, error) in
          if let anError = error{
           // got an error in getting the data, need to handle it
            println("error calling POST on /posts")
            println(error)
    
            callback?(isOk: false)
          }
          else if let data: AnyObject = data{
          // handle the results as JSON, without a bunch of nested if loops
            let post = JSON(data)
           // to make sure it posted, print the results
            println("JSON Returned")
    
            callback?(isOk: true)
          }
        }
    }
    

    然后……

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var identity = Identity(username: "John Apleseed", apiKey: "213123123")
    
        identity.loginPassword { (isOK) -> Void in
            if (isOK) {
                //do good stuff here
            }else{
               // do error handling here
            }
    
        }
    }
    

    更新

    此外,您的调用函数可能如下所示:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var identity = Identity(username: "John Apleseed", apiKey: "213123123")
        identity.loginPassword(handlePasswordRequest)
    }
    

    而且您可以根据需要添加尽可能多的回调处理程序,而不会弄乱一堆嵌套闭包...

    private func handlePasswordRequest(isOK: Bool){
        if (isOK) {
            //do good stuff here
        }else{
            // do error handling here
        }
    }
    

    更新 2

    如果您需要深入调用层次结构的回调,则需要将回调作为每个先前闭包的参数传递。

    更新 3

    我会尝试 RxAlamofire 以及所有关于 RxSwift

    【讨论】:

    • 非常感谢!有没有办法使用 Promise 将它链接在一起,这样它看起来不像嵌套那么讨厌?这可能会在应用程序的另一半达到 4-5 级?
    • 你知道原始答案在 Swift 2 中的样子吗?它在 swift 1.2 中对我有用,但现在似乎删除了错误参数。 Here 是我的问题
    • @HugoAlonso 我很想看到回调示例的 RxAlamofire 嵌套。你能提供一个要点吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2019-07-18
    • 2018-05-12
    • 2020-12-19
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多