[英]Swift Closures: Cannot convert return expression of type '()' to return type 'LiveSearchResponse?'
我有两个控制器。
Maincontroller
Dropdown
Maincontroller
允许用户打开下拉菜单。 Dropdown
允许用户从列表中选择一行。 选定的行值将传递给Maincontroller
。 在Dropdown
控制器中有searchBar。 当用户搜索某些内容时,它会要求Maincontroller
获取用于搜索查询的数据。 Maincontroller
将从Web API获取数据并将结果返回给Dropdown
。
使用闭包将数据从Maincontroller
返回到Dropdown
时,我遇到了问题。
我正在从Maincontroller
模态呈现Dropdown
,如下所示。
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
Web API获取搜索数据的功能。 它返回LiveSearchResponse
类型的Object。
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
我不知道如何从异步函数中获取数据后如何将值返回给Closures。
编辑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 {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.