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

Button click event inside a map marker info window
How do I rotate sprites on a perimeter of a circle towards the center?
Swift: Use guard with or statement
Disable controller in tvOS
Swift nstimer accessing function other class
Can't create a range in Swift 3
Swift: Geofire Instanciation error after Firebase 3 upgrade
Capture lists: reference to class instance or initialized variable?
SQlite Swift unexpected result group
“Type 'task.Type' has no subscript members” swift [duplicate]
UICollectionView behavior is incorrect as Subview
Creating Rounded UILabel in Swift
Swift: Expected ',' separator [closed]
Accessing var from another class or scene in SpriteKit
How to properly use the completionHandler when performing a background fetch?
Accessing an uninstalled constraint programmatically?

Categories

HOME
vue.js
crate
wsdl
laravel-5.3
thunderbird-addon
actionscript
zend-framework2
orientation
jrebel
aruco
onsen-ui2
mongodb-query
phoenix
sendkeys
riak-ts
alfresco-share
pyephem
apply
fop
guile
frp
chrome-native-messaging
aurelia-binding
helix-3d-toolkit
service-locator
bigcartel
xamarin.uitest
haskell-pipes
visualstudio.testtools
tic-tac-toe
twiki
knockout-3.0
pytest-django
upsert
lumen-5.3
recycle-bin
mouseclick-event
ruby-on-rails-2
etherpad
iphone-developer-program
shinyjs
sqldatareader
objloader
project-online
elastix
mfc-feature-pack
fill
interactive-brokers
wikitext
pyopengl
twitter-rest-api
gulp-livereload
stackframe
hiera
chaining
vcl
data-generation
winrt-httpclient
roxygen
healthvault
exiv2
localtime
ienumerator
iconv
shiva3d
android-contextmenu
symfony-2.0
unions
userid
fileutils
electronic-signature
nemerle
out-of-browser
carbide
j-interop
gwt-2.2-celltable
usergroups

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