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

Can I have SKSpriteNodes show up on a different scene during a transition
Can not access an SKSpriteNode from within a function
How to use committedValuesForKeys to save and reconstitute NSManagedObjects?
NSTask putting extra single quote in command
How to get focused view frame size in tvOS?
Swift init with function to make clear separation
How to access a variable value outside of Parse's getObjectInBackgroundWithId block
Nsuserdefaults highscore saving
Conditionally activate UISearchController for iOS8+
How do i pass a dictionary as a function parameter?
Initializing defaults from a file, Swift differs from Obj-C?
How to Sync the particular document from couchbase server?
PHAssetCreationRequest not working
Xcode swift user specific data and access
How do I value bind multiple case statements in Swift 2?
How to make a smooth transition between drum patterns using AVAudioPlayer?

Categories

HOME
json
tensorflow
ember.js
pug
websphere
visual-studio-2013
ubuntu-16.04
mapbox-gl-js
algorithmic-trading
gspread
bro
sudo
currency
structuremap
eclipse-cdt
window
pjsip
rfid
iso
spinnaker
image-recognition
activesync
spring-cloud-config
netezza
zoomcharts
jcl
rails-activerecord
richfaces
iup
qsub
auditing
geopandas
continuous-deployment
upnp
dss
helper
ggmap
dpi
forum
outsystems
stocks
oracle-xml-db
hawtio
mapguide
threshold
openshift-enterprise
parse-android-sdk
magiczoomplus
tic-tac-toe
intellij-idea-2016
knockout-3.0
microdata
constants
android-maps-v2
matlab-cvst
rdw
taskmanager
nstouchbar
livefyre
jenkins-jira-trigger
rhel6
rollback
component-pascal
iphone-developer-program
autoresize
freetype2
android-cursoradapter
sourcegear-vault
matcaffe
qbfc
es2015
consensus
multipleselection
jai
symfony-2.6
em
log4cplus
libssh2
system.web
funkload
unison
bounds
gfs
wic
cling
sharpmap
domain-calculus
mozart-mvc
ninject-extensions
windows-phone-7-emulator
text-services-framework
curljs
testunit
sendfile
ruby-debug
audiostreamer
.app
squeel
gwt-2.2-celltable
rtsp-client
plinq
scatterview
longjmp
hmacsha1
odbc-sql-server-driver





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