gpt4 book ai didi

Swift SearchBar 无法过滤数据

转载 作者:行者123 更新时间:2023-11-30 10:50:49 24 4
gpt4 key购买 nike

我已经设置了 UISearchBar 的东西。但我收到错误消息。

Cannot assign value of type '[cellData]' to type '[String]'

这是代码

struct cellData {
let jobTitle : AnyObject?
let companyName : String?
let jobLocation : String?
let jobDescription : String?
let jobReq : String?
let firstPosterImage : AnyObject?
let secondPosterImage : AnyObject?
let createdAt : Date

init(jobTitle: AnyObject?, companyName: String? = nil, jobLocation: String? = nil ,jobDescription: String? = nil, jobReq:String? = nil, firstPosterImage: AnyObject? = nil, secondPosterImage: AnyObject? = nil, timeStamp:Double) {

self.jobTitle = jobTitle
self.companyName = companyName
self.jobLocation = jobLocation
self.jobDescription = jobDescription
self.jobReq = jobReq
self.firstPosterImage = firstPosterImage
self.secondPosterImage = secondPosterImage
self.createdAt = Date(timeIntervalSince1970: timeStamp/1000)

}

}

var cellDataArray = [cellData]()
var filtered = [String]()


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {


if searchText.isEmpty {
isSearching = false
tableView.reloadData()

} else {
isSearching = true
filtered = cellDataArray.filter({($0.jobTitle?.lowercased?.contains(searchText.lowercased()))!})
tableView.reloadData()
}


}

我在这行遇到错误

filtered  = cellDataArray.filter({($0.jobTitle?.lowercased?.contains(searchText.lowercased()))!})

。请注意,CellDataArray 具有 cellData 结构。我在 tableView 中使用 searchBar。

谢谢。

最佳答案

您的问题是 cellDataArrayfiltered 属于两种不同的数据类型( String 和 struct CellData )所以你不能简单地将一个分配给另一个而不进行像

这样的转换
let res  = cellDataArray.filter({($0.jobTitle?.lowercased?.contains(searchText.lowercased()))!})
filtered = res.map { $0.anyProprtyInside } // property should be a String

您的意思可能是将过滤声明为

var filtered = [CellData]() // start structs / classes / protocols with uppercase letter

关于Swift SearchBar 无法过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54584630/

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