gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:10:46 25 4
gpt4 key购买 nike

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() 方法来获取数据。现在的问题是如何从我的 Controller 调用这个 loadMoreData?

最佳答案

我在这里实现了尾随闭包

loadMoreData(2){ (result) in

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


}

关于ios - 在 Swift 中使用闭包作为完成处理程序时的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com