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

how to continue animation and uitimer in background
Swift 3 Getting Optionals After Initializing Class [duplicate]
Static and lazy variables: similar behaviours?
RxSwift: Observe viewDidLoad from view model without Subjects
How can I export Image as 3DL or Cube File?
core data and relationship predicate
UIScrollView contentSize doesn't work
Unusual high CPU usage in UITableView
How can you test that an index is out of range in Swift?
Run timer in a specific time (swift)
Swift 3 pdf file download not working
Swift allow user to choose player Sprite Node
How to correctly unset an array from UserDefaults in Swift
NSCollectionView `validateDrop` delegate not getting called
How to return early from an init in swift 3
Alamofire.request “Extra argument 'method' in call”

Categories

HOME
sidekiq
sass
puzzle
ubuntu-12.04
survival-analysis
jrebel
filter
proguard
octopus-deploy
xamarin-studio
cakephp-2.9
symfony-forms
datagrip
cloudflare
jquery-terminal
alfresco-share
arraylist
explode
fabric
badge
python-textprocessing
switching
image-quality
superagent
blackboard
crt
freebase
polymorphism
nsurlconnection
swagger-editor
poco-libraries
strstr
code-behind
sendgrid-templates
ivona
openweathermap
timesten
appstore-approval
jenkins-jira-trigger
workflow-foundation-4.5
slot
mbaas
festival
inject
ng2-material
scriptlet
proxygen
xmp
cmocka
spservices
loose-typing
visual-studio-code
twython
stream-framework
gray-code
ibm-data-studio
uos
web-component-tester
uptodate
custom-url
node-inspector
formvalidation-plugin
coin-flipping
mfmailcomposeviewcontroll
cgimageref
fileconveyor
zend-lucene
fluidsynth
sametime
trailing-slash
caliper
separation-of-concerns
driving-directions
viewdidload
ubuntu-11.04
shared-objects
gtk2hs
dashcode
getresource
revert
boost-date-time
oggvorbis
kpi
associativity





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss