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

swift - UICollectionView issue “fatal error: unexpectedly found nil while unwrapping an Optional value”
Search Bar - filtering numeric value in swift
error: missing argument for parameter #1 in call (swift)
NSURLSession concurrent requests with Alamofire
Why need get/set when using Computed Properties in Swift, while we can code without them actually?
Trying to conform to StringLiteralConvertible
LLDB swift print object
Changing position.x causes skspritenode to not display
Wait until an asynchronous api call is completed - Swift/IOS
Read lines from big text file in Swift until new line is empty: the Swift way
how do i enable autofill password for a wkwebview
Multidimensional dictionaries possible in Swift?
Access instance constant in lazy constructor
Typecase regular Swift function to Curry Function
What's the most efficient way to determine if a Swift string is shorter than a certain length
Swift: array of dictionaries has count of 1 after initialization but should have 0 [duplicate]

Categories

HOME
app-inventor
puzzle
winapi
itext
google-tag-manager
jbehave
decorator
qpython3
mapbox-gl-js
is-empty
user
sequelize.js
browserify
diagram
cartodb
pjsip
wysiwyg
apache-karaf
thumbnails
multiple-columns
servicemix
extractor
tfs2013
iis-10
non-deterministic
java-ee-7
hash-collision
codelite
chrome-native-messaging
multichoiceitems
vertex-buffer
blackboard
honeysql
css-counter
magiczoomplus
silverlight-3.0
ntfs-mft
fax
alchemy.js
soundjs
filepath
range-v3
number-theory
mouseclick-event
applepayjs
workflow-foundation-4.5
ticker
photon-controller
execl
static-code-analysis
teiid
vici
packagemaker
easing
mix
zurb-foundation-apps
mongoskin
gmsmapview
project-online
mongo-c-driver
qt-linguist
altbeacon
servlet-3.0
acm
cctray
python-winshell
divide-by-zero
crash-dumps
google-hadoop
uptodate
pgagent
line-numbers
adcolony
cg
google-admin-audit-api
mov
csplit
adaptive-compression
dbsetup
vertical-scrolling
objcmongodb
fluidsynth
domain-calculus
qsqltablemodel
feof
mpmovieplayer
macruby
hibernate3
cross-domain-policy
webkit-transform
electronic-signature
nemerle
adobe-contribute
pivotal-crm
rtsp-client
interface-design
visual-studio-dbpro

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