I will not be able to return the value using Alamofire in Swift

In the current code, which I don’t have, it seems to return nothing, I can’t find out what causes the problem.

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 anyone know how to solve the problem I have?

[Refresh] . This is the status.

Look at the fifth and eighth lines. I can’t get the quest quest.

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

Thank.

+4
source share
3 answers

The other answers are certainly correct and touch upon many of the problems you encounter with asynchronous operations. I just wanted to add that a call is dispatch_async(dispatch_get_main_queue())not required.

Alamofire. Alamofire . (, ..), . dispatch_async .

, , , , , .

.

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)")
}
+5

, . , , " ", .

, "getQuests", , :

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

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                let quests = json as? NSArray
                // pass the array of quests, or an empty array to your completionHandler
                completionHandler(quests ?? [])
            })
    }
}

- , - :

getQuests("Easy", count: 5, completionHandler: {
    quests in
        println(quests)
    })

, .

+2

, .

var quests = NSArray()  

, . . , , .

func getQuests(category: NSString, count: Int) {

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

All Articles