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 why does as? require AnyObject vs Any
Unable to play video using AVPlayer with Swift
2 decimal timer in swift
must call a designated initializer of the superclass NSManagedObject - in swift
i need The app regularly downloads and processes small amounts of content from the network(swift)
xcode 7.3 Chinese localization does not work with iOS 8.x
Firebase iterate snapshot in Swift
Swift 2 - UITableView with Multi sections
Swift - Can a class conform some protocol programmatically?
Find index of dictionary entry in Swift
Open a View that is not the root one and keep the tabbarcontroller from the root view (swift)
Uploading multiple Images to Parse
Admob ad sometimes plays when app loads
How to create the effect of a circular object entering and separating from a thick substance
Write down the time when app is closed or enters backgound
Disable time seek in AVPlayer

Categories

HOME
tinyos
sed
warnings
performancecounter
google-tag-manager
json-ld
jscript
tizen-wearable-sdk
ng-admin
eclipse-cdt
maxima
event-log
pdo
scenebuilder
cvs2svn
flexboxgrid
fop
source-maps
tflearn
tfs2013
philips-hue
iis-10
sumo
ui5
crt
spreedly
von-neumann
viewstate
pox
nunit-3.0
broadleaf-commerce
flex4.5
intellij-idea-2016
google-account
stress-testing
amd
context-free-language
chrome-remote-desktop
react-native-fbsdk
paas
rxvt
emgu
system.web.ui.webcontrols
backstop.js
rstudio-server
tuxedo
django-filer
boost-hana
shipitjs
spatial-query
spring-lemon
elastix
asynccallback
fill
altbeacon
schtasks.exe
document-oriented-db
insert-into
app.xaml
mysqldumpslow
qbfc
apigee-baas
twython
swift2.1
code-first-migrations
multipleselection
python-ggplot
pisa
cocoascript
ultrawingrid
vcl
google-admin-audit-api
io.js
dbsetup
factors
surveyor-gem
digital-design
zend-lucene
bulbs
socketstream
tfs-power-tools
pstree
shim
unions
getstring
z-machine
file-exists
userid
text-size
port-scanning
audiostreamer
rtsp-client
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