ios - Swift 闭包:无法将类型“()”的返回表达式转换为返回类型“LiveSearchResponse?”
问题描述
我有两个控制器。
Maincontroller
Dropdown
Maincontroller
允许用户打开下拉菜单。
Dropdown
允许用户从列表中选择一行。选定的行值将传递给Maincontroller
. 在Dropdown
控制器中有searchBar。当用户搜索某些东西时,它要求Maincontroller
获取搜索查询的数据。Maincontroller
将从 Web API 获取数据并将结果返回到Dropdown
.
从使用闭包返回数据时Maincontroller
,我遇到了这个问题。Dropdown
我Dropdown
从Maincontroller
下面模态地介绍。
let searchVC = LiveSearchDropDown<DropDownTitleTCell>.init(configureCell: { (cell ,object) -> DropDownTitleTCell in
cell.lblTitle.text = "Dummy"
return cell
}, selection: { (selectedObject) in
print(selectedObject)
self.dismiss(animated: false, completion: nil)
}, search: { (query, spaging) -> LiveSearchResponse? in
let res = self.fetchPatients(query: query, forPaging: spaging, response: { (response) in
})
return res
})
self.present(searchVC, animated: true)
以下是我从Maincontroller
. 它返回类型的对象LiveSearchResponse
。
func fetchPatients(query searchText: String, forPaging : Paging, response: @escaping(LiveSearchResponse) -> ()) {
let params = Prefs.getAPICallParameters()
var responseData = LiveSearchResponse()
APIManager.shared.jsonRequest(url: AppConstant.Patient.getPatientList, parameters: params, method: .post, encoding: JSONEncoding.default, onSuccess: { (resposeJSON) in
if let patientList = resposeJSON["data"].array {
if patientList.count > 0 {
var data = [Patient]()
//success retreived
for patient in patientList {
data.append(Patient(json: patient))
}
if patientList.count < 20 {
forPaging.shouldLoadMore = false
} else {
forPaging.shouldLoadMore = true
}
responseData.data = data
responseData.error = nil
} else {
forPaging.status = .failed
}
response(responseData)
} else {
forPaging.status = .failed
self.presentAlertWithTitle(title: "Error", message: "AppConstant.Patient.getPatientList data Key not found", options: "Ok", completion: { (option) in
})
response(responseData)
}
}) { (error) in
forPaging.status = .failed
self.presentAlertWithTitle(title: "Error", message: error.message, options: "Ok", completion: { (option) in
})
response(responseData)
}
}
当我从关闭返回对象时,我在下面的块上遇到编译时错误。
无法将类型“()”的返回表达式转换为返回类型“LiveSearchResponse?”
search: { (query, spaging) -> LiveSearchResponse? in
let res = self.fetchPatients(query: query, forPaging: spaging, response: { (response) in
})
return res
从异步函数获取数据后,我不知道如何将值返回给闭包。
编辑 2
在Dropdown
我宣布喜欢
public let searchQuery: LiveSearchQuery
typealias LiveSearchQuery = (String, Paging) -> LiveSearchResponse?
初始化
required init(configureCell: @escaping CellConfiguration, selection: @escaping Selection, search: @escaping LiveSearchQuery) {
self.configureCell = configureCell
self.selection = selection
self.searchQuery = search
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .formSheet
self.modalTransitionStyle = .crossDissolve
self.preferredContentSize = CGSize(width: 400, height: 400)
}
并称它为
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let query = searchBar.text, !query.isEmpty {
self.paging.status = .loading
self.tableView.reloadData()
let response = searchQuery(query, self.paging)
if response?.error == nil {
self.dataModels = response?.data as? [AnyObject]
} else {
}
self.tableView.reloadData()
searchBar.resignFirstResponder()
} else {
}
}
你能建议我归档目标的正确方法吗?
解决方案
如果您正在调用异步方法,那么search
不应该返回,您应该传递一个完成块,我认为它看起来像这样:
search: {[weak self] (query, spaging, completion) in
self?.fetchPatients(query: query, forPaging: spaging, response: { (response) in
completion(response)
})
编辑 1
我对添加的信息有了更多的想法,你应该能够做这样的事情:
public let searchQuery: LiveSearchQuery
typealias LiveSearchQuery = (String, Paging, @escaping (LiveSearchResponse?)->())->()
required init(configureCell: @escaping CellConfiguration, selection: @escaping Selection, search: @escaping LiveSearchQuery) {
self.configureCell = configureCell
self.selection = selection
self.searchQuery = search
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .formSheet
self.modalTransitionStyle = .crossDissolve
self.preferredContentSize = CGSize(width: 400, height: 400)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let query = searchBar.text, !query.isEmpty {
self.paging.status = .loading
self.tableView.reloadData()
searchQuery(query, self.paging) { [weak self] response in
DispatchQueue.main.async {
if response?.error == nil {
self?.dataModels = response?.data as? [AnyObject]
} else {
}
self?.tableView.reloadData()
searchBar.resignFirstResponder()
}
}
} else {
}
}
推荐阅读
- java - Rest API 返回 byte[] i 响应
- .net - WS-Federation 声明身份验证 (MVC.Net)
- c# - WebBrowser 检测文件下载事件
- regex - Sonatype NXRM - 资产名称匹配器
- sql-server - 删除程序(如果存在)
不管用 - c - C打印指向数组的指针地址问题
- python - 尝试使用 seaborn 绘制散点图,但会引发一些错误
- react-native - 在 react-navigation v5 中切换导航器
- javascript - 我们可以获得所有具有 data-ng-click 的锚点列表吗?
- python - Django shell 无法执行 ORM 查询