[英]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.