swift


searchbar textDidChange error


I am trying to implement a searchbar for a tableView and I am receiving the error "...Binary operator '==' cannot be applied to operands of type 'Place' and 'String'" in my textDidChange method. The tableView is populated from a Firebase database "placeList" array. Not sure where the error source is coming from. Thanks in advance for any help!
lazy var searchBar:UISearchBar = UISearchBar()
var placeList = [Place]()
var placesDictionary = [String: Place]()
var isSearching = false
var filteredData = [Place]()
override func viewDidLoad() {
super.viewDidLoad()
searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.placeholder = " Search Places..."
searchBar.sizeToFit()
searchBar.isTranslucent = false
searchBar.backgroundImage = UIImage()
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
navigationItem.titleView = searchBar
tableView.allowsMultipleSelectionDuringEditing = true
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
if isSearching {
cell.textLabel?.text = filteredData[indexPath.row].place
} else {
cell.textLabel?.text = placeList[indexPath.row].place
}
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
tableView.reloadData()
} else {
isSearching = true
// error in below line of code...
filteredData = placeList.filter({$0.place == searchBar.text})
tableView.reloadData()
}
}
Your property placeList is an array of Place objects. When you call the filter function on your array (placeList.filter({$0 == searchBar.text!})), what you are saying is "filter placeList where a Place object is equal to searchBar.text". A place object is not a String, you cannot compare two different types. I'm not familiar with your data model, or your Place class, but maybe you have some type of String property in your Place class which you could use to compare? For instance, say Place had a property called id of type String, you could then filter through comparison like so: filteredData = placeList.filter({$0.id == searchBar.text!}) - notice the added $0.id.
You can only compare a String to a String

Related Links

The main thread locked when fetching images from parse with semaphore
How do you make a transparent view that changes color [closed]
swift uploadTaskWithRequest with didReceiveData
stringByReplacingOccurrencesOfString removes whole string
Why can I use a constant initialization as if-condition? [duplicate]
How to convert Optional String to String
Unwrap an Optional Within a Multiple Parameter Function
Swift on Ubuntu - No such file or directory
Swift: programmatically enumerate outgoing segues from a UIVIewController
Can't get plist URL in Swift
Getting data from Parse in condition
How to assign an image from URL to NSData by swift?
Swift append String var to an List by reference
SearchBar & Display in table view with Coredata using Swift?
How to delay the start of a function
Swift Shrink Animation

Categories

HOME
crystal-reports
vue.js
sidekiq
semantic-ui
dynamics-crm
voip
wsdl
ncurses
antivirus
automated-tests
contact-form-7
aruco
lotus-notes
getorgchart
finite-group-theory
mongodb-query
local
ejs
netezza
uicollectionview
bar-chart
tree-traversal
mousewheel
cython
flexboxgrid
google-ima
facebook4j
source-maps
memsql
exponential
geo
modulo
node-horseman
bing-maps-api
sql-like
stdclass
ical-dotnet
automator
aqgridview
proof
walmart-electrode
azureportal
maintenance
tofixed
quintus
launch
selenium-firefoxdriver
jcreator
teiid
onresume
createobject
svn-merge
project-template
gradle-script-kotlin
cleditor
debugdiag
glkit
mongoose-populate
software-product-lines
try-finally
faraday
asp.net-mvc-partialview
cmocka
brackets-shell
difference
sonarqube5.3
data-import
hornetq
base32
risk-analysis
twitter-rest-api
isml
android-search
pgagent
odftoolkit
suffix-array
dalekjs
outline
mbox
facebook-chat
bigint
fogbugz-api
goinstant
jqmobi
ftp4j
nuspec
manage.py
dynamics-ax-2009
fortran77
device-emulation
static-variables
getusermedia
icefaces-3
hibernate3
datamember
goliath
rubycas
ihttphandler
audiostreamer
svn-hooks
surefire
scala-2.8
graniteds
iphone-sdk-3.2
openwysiwyg
kpi
inline-if
data-execution-prevention

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App