我正在尝试从我的 API 加载自定义数据类型并将其显示在我的 iOS 应用程序登录页面上。
据我了解,我应该这样称呼:
override func viewDidLoad() {
performSelector(inBackground: #selector(fetchJSON), with: nil)
let myCustomDataType = //how so I get this back
tableView.reloadData()
//...
}
Run Code Online (Sandbox Code Playgroud)
显然 fetchJSON 的声明应该在另一个文件中,所以我不会用它不需要做的事情来填充我的控制器。但是,我需要此函数来返回 [MyCustomDataType] 列表并将其显示在我的登录页面上。
func fetchJSON() -> [MyCustomDataType] {
//get api, fetch data, put it in my custom data type list
return myCustomDataType
}
Run Code Online (Sandbox Code Playgroud)
我使用闭包吗?创建全局 [MyCystomDataType]。或者我如何实现这一目标?请注意异步任务,因为我显示的表格单元格有点像 facebook 或 instagram 新闻提要页面。