I won't be able to return a value with Alamofire in Swift

前端 未结 3 521
别跟我提以往
别跟我提以往 2020-12-11 21:48

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


        
3条回答
  •  天命终不由人
    2020-12-11 22:52

    You need to make quests a property on your class, so that you can access it from within the async callback.

    var quests = NSArray()  
    

    You're aren't going to be able to return the array, because it is async. Just remove the return type. When the callback fires, save the results to your array, and do whatever else you want to happen.

    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)  #=> ()
                    })
            }
    }
    

提交回复
热议问题