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

I need to set a notification on a specific day every week.. but I don`t care the day of the month
Xcode Swift Quick Help Closures
Swift Inherited Protocols and Conformance Checking for the Parent-Protocol
Copy NSTableView row greyed out
How to find out if card was declined in Braintree Payments
how to create a class and return something. Swift
Call custom action in spritekit around 100 times in 3 seconds
Firebase Storage Upload Error: FIRStorageErrorDomain Code=-13000
Use of instance member 'meow' on type 'Cat' did you mean to use a value of type 'Cat' instead?
Correct way to redraw NSTextView
In Swift, how can I get the row of the cell in which a button was pressed?
Xcode, mapped executable file has no team identifier in its signature
Swift - How to set the image of a UIBarButtonItem in a custom class
NSWorkSpace Notification. Receive notification from Skype when call is being received
Custom UITableViewCell programmatically with SWIFT
How display UIImage in swift using struct

Categories

HOME
dotnetrdf
caching
crystal-reports
payment-gateway
xamarin.android
amazon-swf
system-verilog
wsdl
hana
mainframe
specflow
ipfs
google-classroom
uiactivityviewcontroller
ssr
abcpdf
cs-cart
nuxt.js
webmethods
rapidjson
riak
uicollectionview
grub2
jpeg2000
ms-access-2007
jogl
plupload
pycrypto
jni
excel-2010
jquery-form-validator
spring-ws
iup
remove-method
go-cd
dss
sharp
gpib
swiftcharts
rider
boilerplate
wfp
zero
codesys
sonarqube-scan
magiczoomplus
pdftk
rkt
php-ews
w3-total-cache
web-deployment-project
shutdown
opencpu
shinyjs
push-diffusion
rstudio-server
ruby-2.0
visible
scalar
jackson-databind
gemini
freefem++
magento-1.4
except
lemoon
kendo-menu
futuretask
apigee-baas
bridge
searchkick
sysfs
obfuscar
inputaccessoryview
java-melody
ultrawingrid
method-overriding
aquafold
neoload
ifft
exiv2
sim900
vertical-rhythm
sql-server-2012-web
lame
iconv
magickwand
fluidsynth
icsharpcode
vim-powerline
expression-evaluation
hinstance
path-manipulation
nsindexpath
associative
collect
post-redirect-get
openvg
gears

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