我将无法在Swift中使用Alamofire返回值 [英] I won't be able to return a value with Alamofire in Swift

查看:178
本文介绍了我将无法在Swift中使用Alamofire返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的当前代码似乎未返回任何内容,我找不到导致问题的原因。

The current code I'm having doens't seem to return anything, I can't find out what is causing the issue.

func getQuests(category: NSString, count: Int) -> NSArray {
    var quests = NSArray()

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                quests = json as NSArray
            })
    }

    println(quests)  #=> ()

    return quests
}

有人知道如何解决我遇到的问题?

Does anybody know how to solve the issue I'm having?

[更新] :这是状态。

请查看第五行和第八行。
我无法分配任务。

Please look at the fifth and eight row. I can't get the assignment to quests work.

var quests = NSArray()

getQuests("normal", count: 30, completionHandler: {
    quests in
        self.quests = quests
    })

println(self.quests)  #=> ()

func getQuests(category: NSString, count: Int, completionHandler: (NSArray -> Void)) {
    var quests = NSArray()

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                quests = json as NSArray
                completionHandler(quests)
            })
    }
}

谢谢。

推荐答案

其他答案当然正确无误关于异步操作所遇到的许多问题。我只想补充一下这样的事实,即不必调用 dispatch_async(dispatch_get_main_queue())

The other answers are certainly correct and hit on many of the issues you are running into with asynchronous operations. I'd merely like to add the fact that the dispatch_async(dispatch_get_main_queue()) call is not necessary.

这已经在Alamofire内部自动完成。 Alamofire处理内部委托调度队列上的所有操作。一旦完成所有这些操作(验证,响应序列化等),默认情况下,将在主调度队列上调用完成处理程序关闭。这使得 dispatch_async 不必要,应该将其删除。

This is already being done automatically inside of Alamofire. Alamofire handles all operations on an internal delegate dispatch queue. Once all those operations are completed (validation, response serialization, etc.), your completion handler closure is called on the main dispatch queue by default. This makes the dispatch_async unnecessary and it should be removed.


您也可以运行如果愿意,您可以在自己提供的调度队列中使用完成处理程序,但这肯定是高级功能,不适用于该用例。

You can also run your completion handlers on your own provided dispatch queue if you like, but that's certainly an advanced feature that is not applicable to this use case.



Here's a more concise version of the same logic.

let apiUrlString = "some/url/path"

func getQuests(#category: NSString, count: Int, completionHandler: (NSArray) -> Void) {
    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
             .responseJSON { _, _, json, _ in
                 completionHandler(json as NSArray)
             }
}

var myQuests: NSArray?

getQuests(category: "normal", count: 30) { quests in
    myQuests = quests
    println("My Quests: \(myQuests)")
}

这篇关于我将无法在Swift中使用Alamofire返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆