Swift SearchBar无法过滤数据

时间:2019-02-08 00:53:33

标签: swift xcode swift4 searchbar

我已经设置了UISearchBar的东西。但是我说错了。

  

无法将类型[[cellData]”的值分配为类型[[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。

谢谢。

1 个答案:

答案 0 :(得分:0)

您的问题是insertOnecellDataArray属于2种不同的数据类型(filtered和struct String),因此您不能简单地将一个分配给另一个像

这样的转化
CellData

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

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