在Swift中使用闭包作为完成处理程序时的返回值

时间:2015-08-04 06:12:33

标签: ios swift completionhandler

func loadMoreData(offset: Int, completion: (result: [ArtistJSONMapper]?) -> Void) {

    var fetchedData = [ArtistJSONMapper]()
    let pageNum: Int = offset/paging.limit

    // Calling the json fetch to obtain data

    JSONFetch.jsonTest() { (fetched, error) -> Void in
        if(fetched != nil) {
            fetchedData = fetched!
            //self.tableView.reloadData()

        } else {
            println("error - \(error)")
        }
    }

    println("Fetched data count is \(fetchedData.count)")
    completion(result: fetchedData.count > 0 ? fetchedData : nil)

}

我正在使用AlamofireObjectMapper使用JSONFetch.jsonTest()方法获取数据。现在的问题是如何从我的控制器调用这个loadMoreData?

1 个答案:

答案 0 :(得分:0)

我在这里实现尾随闭包

loadMoreData(2){ (result) in

//after completion of your work this closure gets called
 println("your return \(result)")


}