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

Iterate an RLMArray in Swift
EXC_BAD_ACCESS on waitForExpectations while unit testing
How do I pass a class as a parameter in Swift?
Mac app unable to read app bundle resources
randomly picking skspritenode node to spawn out of 5 sprites
Superimpose a user filled out UITextView over an image with CGRect, not the same as combining two images?
Add a top padding to IoS app WKWebView
Translating Obj-C blocks into Swift for AFNetworking calls
How to make post request in CURL with Perfect
Get Street Address From Contact Swift
Get name of closest business using Swift's CoreLocation
Swift dealing with Actions and Selectors Issue -
How to Save Data and Segue Back to Collection View?
&& and || in while loops
Collision dectection acting randomly
How to edit values in Dictionaries in Swift

Categories

HOME
rest
caching
crate
puppet
amazon-product-api
docker-swarm
safari
flyway
propertygrid
branch.io
simpy
requirejs
structuremap
dropbox
wicket
cartodb
simple-injector
ssms-2016
bootstrap-popover
scenebuilder
saiku
jogl
linq-to-sql
uiautomator
cruisecontrol.net
ghost-inspector
memsql
myob
rhmap
metis
gpib
svnkit
construct-2
google-guava-cache
bigcartel
swagger-editor
delphi-10.1-berlin
alchemy.js
socketscan
productivity
excel-interop
pytest-django
finite-element-analysis
python-idle
proof
number-theory
defold
dimple.js
windows-firewall
appstore-approval
xmgrace
cgcontext
dwarf
decompress
volume
gulp-typescript
nsfetchedresultscontrolle
coremidi
c64
uistackview
vst
unsatisfiedlinkerror
dojox.charting
mongo-c-driver
financial
asp.net-mvc-2
handlebars.java
android-cursoradapter
hornetq
oai
calibration
leadtools-sdk
harp
findcontrol
lines-of-code
gulp-livereload
callstack
robocode
jython-2.7
magento-1.12
google-earth-plugin
belongs-to
ember-components
java-collections-api
sthttprequest
valence
cppdepend
joox
config-files
late-static-binding
json-patch
facebook-chat
facebook-sdk-3.1
.net-cf-3.5
sttwitter
html-form-post
pep8
tridion2009
cadisplaylink
subtract
flexicious
hinstance
executable-format
posting
opengl-es-lighting
search-path
plinq
inline-if
weborb
rd

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