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

custom console log in swift [duplicate]
UI Test Success but never Runs on Mac
How to fake Realm Results for tests
Swift wait until dataTaskWithRequest has finished to call the return [duplicate]
Making a GIF from images using Swift (macOS)
NSTableView cells ontop of header
CoreData/TableView - Ambiguous use of 'subscript'
Saving the users username to the firebase database using swift in Xcode
Swift: Help finding inconsistencies that are making my app crash [duplicate]
Correct release NSViewController embedded in subviews
How to fetch stickers collection from iMessage?
Adding constraint (programmatically) for NSView never returns
Java BigDecimal in Swift
Under what situation Swift Cannot infer the type to be use?
What is the correct way to extend the functionality of a CocoaPod library?
Infer closure return type from closure body when working with generics

Categories

HOME
xbox-live
cakephp-3.x
crystal-reports
sass
shopify
drupal-7
boost-thread
angular2-routing
cpu-architecture
qpython3
google-sheets-api
jscript
javamail
specflow
schemacrawler
qt-installer
equalizer
medium-editor
flat-file
countif
cocoa-touch
richfaces
tarantool
nmf
bootstrap-typeahead
searchbar
java-ee-7
image-quality
bitcoin-testnet
red-black-tree
svnkit
spring-profiles
receipt
geo
swiftcharts
npm-shrinkwrap
transient
service-locator
nunit-3.0
ksoap
wfp
redhat-datavirt
discord.net
strstr
stdclass
nsexception
productivity
jrules
bing-translator-api
ivona
rich-text-editor
defold
nstouchbar
livefyre
gauss
pinvoke
spell-checking
struts-layout
slidesjs
tcserver
mcrypt
communication-protocol
pspice
android-navigationview
pdflib
picasa
cron-task
try-finally
bacnet
pax-web
twgl.js
asynccallback
futuretask
apigee-baas
visual-studio-code
pyrocms
mptcp
ffprobe
swift2.1
odftoolkit
vcl
dalekjs
dto
funq
adaptive-compression
coin-flipping
neoload
colt
codeigniter-a3m
indexing-service
resource-files
openkinect
pyjade
bfd
fogbugz-api
facebook-sdk-3.1
sslexception
goinstant
carddav
mstsc
fileconveyor
fortran77
zend-framework-modules
tridion2009
symfony-2.0
wcf-web-api
supersized
paintcomponent
osx-leopard
oggvorbis
revision
web-analytics-tools
ajaxpro
divx

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