SearchBar Filter base on Object name - Swift 4
up vote
-2
down vote
favorite
I have a search bar with these data format as a Realm object.
Optional(Results<Place> <0x7fb0d9e0bb10> (
[0] Place {
name = Federal Street;
country = United States;
lat = 42.5447229;
lon = -71.2809886;
},
...
I'm trying to make the filter working
//MARK: - Search bar methods
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText (searchText)")
places = places.filter { ($0["name"] ?? "").range(of: searchBar.text ?? "", options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
I kept getting
Cannot subscript a value of incorrect or ambiguous type
Any hints on what I did wrong ?
ios swift realm uisearchbar uisearchbardelegate
add a comment |
up vote
-2
down vote
favorite
I have a search bar with these data format as a Realm object.
Optional(Results<Place> <0x7fb0d9e0bb10> (
[0] Place {
name = Federal Street;
country = United States;
lat = 42.5447229;
lon = -71.2809886;
},
...
I'm trying to make the filter working
//MARK: - Search bar methods
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText (searchText)")
places = places.filter { ($0["name"] ?? "").range(of: searchBar.text ?? "", options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
I kept getting
Cannot subscript a value of incorrect or ambiguous type
Any hints on what I did wrong ?
ios swift realm uisearchbar uisearchbardelegate
1
In$0["name"]
,$0
is aPlace
object, no? So I guess it should be$0.name
depending on how is definedPlace
Also,table.reloadData()
should also be reload in Main Queue, even if in theory theUISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.
– Larme
Nov 8 at 17:27
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because thefilter
method you used returns anArray
, butplaces
is defined as being a LazyCollectionArray
– Larme
Nov 8 at 18:03
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have a search bar with these data format as a Realm object.
Optional(Results<Place> <0x7fb0d9e0bb10> (
[0] Place {
name = Federal Street;
country = United States;
lat = 42.5447229;
lon = -71.2809886;
},
...
I'm trying to make the filter working
//MARK: - Search bar methods
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText (searchText)")
places = places.filter { ($0["name"] ?? "").range(of: searchBar.text ?? "", options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
I kept getting
Cannot subscript a value of incorrect or ambiguous type
Any hints on what I did wrong ?
ios swift realm uisearchbar uisearchbardelegate
I have a search bar with these data format as a Realm object.
Optional(Results<Place> <0x7fb0d9e0bb10> (
[0] Place {
name = Federal Street;
country = United States;
lat = 42.5447229;
lon = -71.2809886;
},
...
I'm trying to make the filter working
//MARK: - Search bar methods
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText (searchText)")
places = places.filter { ($0["name"] ?? "").range(of: searchBar.text ?? "", options: [ .caseInsensitive, .diacriticInsensitive ]) != nil }
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
I kept getting
Cannot subscript a value of incorrect or ambiguous type
Any hints on what I did wrong ?
ios swift realm uisearchbar uisearchbardelegate
ios swift realm uisearchbar uisearchbardelegate
edited Nov 9 at 19:12
asked Nov 8 at 17:23
kyo
17.4k37128228
17.4k37128228
1
In$0["name"]
,$0
is aPlace
object, no? So I guess it should be$0.name
depending on how is definedPlace
Also,table.reloadData()
should also be reload in Main Queue, even if in theory theUISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.
– Larme
Nov 8 at 17:27
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because thefilter
method you used returns anArray
, butplaces
is defined as being a LazyCollectionArray
– Larme
Nov 8 at 18:03
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11
add a comment |
1
In$0["name"]
,$0
is aPlace
object, no? So I guess it should be$0.name
depending on how is definedPlace
Also,table.reloadData()
should also be reload in Main Queue, even if in theory theUISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.
– Larme
Nov 8 at 17:27
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because thefilter
method you used returns anArray
, butplaces
is defined as being a LazyCollectionArray
– Larme
Nov 8 at 18:03
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11
1
1
In
$0["name"]
, $0
is a Place
object, no? So I guess it should be $0.name
depending on how is defined Place
Also, table.reloadData()
should also be reload in Main Queue, even if in theory the UISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.– Larme
Nov 8 at 17:27
In
$0["name"]
, $0
is a Place
object, no? So I guess it should be $0.name
depending on how is defined Place
Also, table.reloadData()
should also be reload in Main Queue, even if in theory the UISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.– Larme
Nov 8 at 17:27
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because the
filter
method you used returns an Array
, but places
is defined as being a LazyCollectionArray– Larme
Nov 8 at 18:03
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because the
filter
method you used returns an Array
, but places
is defined as being a LazyCollectionArray– Larme
Nov 8 at 18:03
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
What also should work is if you are using NSPredicate
let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
places.filter(using: bPredicate)
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
add a comment |
up vote
0
down vote
This is what I did
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
places = places?.filter("name CONTAINS[c] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
and it is working perfectly
Hope this will help someone like me in the future.
add a comment |
up vote
-2
down vote
Filter can be like:
places = places.filter({
$0
.name
.uppercased()
.components(separatedBy: .whitespaces)
.filter { $0.hasPrefix(searchText.uppercased())}
.count > 0
}).sorted(by: { $0.name > $1.name })
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try changeplaces = places.filter({
and.filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
|
show 1 more comment
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
What also should work is if you are using NSPredicate
let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
places.filter(using: bPredicate)
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
add a comment |
up vote
1
down vote
accepted
What also should work is if you are using NSPredicate
let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
places.filter(using: bPredicate)
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
What also should work is if you are using NSPredicate
let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
places.filter(using: bPredicate)
What also should work is if you are using NSPredicate
let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
places.filter(using: bPredicate)
answered Nov 9 at 14:02
Retterdesdialogs
2,53911534
2,53911534
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
add a comment |
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Code clean, compile fine, but when I start typing, I don't see filter working. Anything I missed ?
– kyo
Nov 9 at 18:36
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
Result : i.imgur.com/LRaY9Wm.gif
– kyo
Nov 9 at 18:38
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
@kyo Hi Kyo, sorry I was ill last week. Did it work for you now? Oh I see you modified the filter in your own answer a little bit :)
– Retterdesdialogs
Nov 19 at 9:02
It works I accepted it
– kyo
Nov 20 at 14:45
It works I accepted it
– kyo
Nov 20 at 14:45
add a comment |
up vote
0
down vote
This is what I did
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
places = places?.filter("name CONTAINS[c] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
and it is working perfectly
Hope this will help someone like me in the future.
add a comment |
up vote
0
down vote
This is what I did
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
places = places?.filter("name CONTAINS[c] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
and it is working perfectly
Hope this will help someone like me in the future.
add a comment |
up vote
0
down vote
up vote
0
down vote
This is what I did
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
places = places?.filter("name CONTAINS[c] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
and it is working perfectly
Hope this will help someone like me in the future.
This is what I did
extension PlacesVC : UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
places = places?.filter("name CONTAINS[c] %@", searchBar.text!).sorted(byKeyPath: "name", ascending: true)
if searchBar.text?.count == 0 {
load()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
table.reloadData()
}
}
and it is working perfectly
Hope this will help someone like me in the future.
answered Nov 9 at 18:53
kyo
17.4k37128228
17.4k37128228
add a comment |
add a comment |
up vote
-2
down vote
Filter can be like:
places = places.filter({
$0
.name
.uppercased()
.components(separatedBy: .whitespaces)
.filter { $0.hasPrefix(searchText.uppercased())}
.count > 0
}).sorted(by: { $0.name > $1.name })
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try changeplaces = places.filter({
and.filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
|
show 1 more comment
up vote
-2
down vote
Filter can be like:
places = places.filter({
$0
.name
.uppercased()
.components(separatedBy: .whitespaces)
.filter { $0.hasPrefix(searchText.uppercased())}
.count > 0
}).sorted(by: { $0.name > $1.name })
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try changeplaces = places.filter({
and.filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
|
show 1 more comment
up vote
-2
down vote
up vote
-2
down vote
Filter can be like:
places = places.filter({
$0
.name
.uppercased()
.components(separatedBy: .whitespaces)
.filter { $0.hasPrefix(searchText.uppercased())}
.count > 0
}).sorted(by: { $0.name > $1.name })
Filter can be like:
places = places.filter({
$0
.name
.uppercased()
.components(separatedBy: .whitespaces)
.filter { $0.hasPrefix(searchText.uppercased())}
.count > 0
}).sorted(by: { $0.name > $1.name })
edited Nov 9 at 18:53
kyo
17.4k37128228
17.4k37128228
answered Nov 8 at 18:19
Grigory S Shushakov
272
272
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try changeplaces = places.filter({
and.filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
|
show 1 more comment
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try changeplaces = places.filter({
and.filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
However it is not working as intedned. The filter functionality is not triggered. See this giff : i.imgur.com/yp8fciX.gif
– kyo
Nov 8 at 18:31
Try change
places = places.filter({
and .filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Try change
places = places.filter({
and .filter { $0.hasPrefix(searchText.uppercased())}
– Grigory S Shushakov
Nov 8 at 19:28
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Just copy my peace again :)
– Grigory S Shushakov
Nov 8 at 20:15
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
Use searchText, for filtering.
– Grigory S Shushakov
Nov 8 at 21:10
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
searchText is not exist. : i.imgur.com/83tMAtj.png
– kyo
Nov 8 at 23:40
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213076%2fsearchbar-filter-base-on-object-name-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
In
$0["name"]
,$0
is aPlace
object, no? So I guess it should be$0.name
depending on how is definedPlace
Also,table.reloadData()
should also be reload in Main Queue, even if in theory theUISearchBarDelegate
should be called in Main Thread, there is no need to resign the searchBar first responder with a dispatch async.– Larme
Nov 8 at 17:27
@Larme : I have try the dot notation, and still have no luck, see this : i.imgur.com/5ivinrj.png
– kyo
Nov 8 at 18:01
stackoverflow.com/questions/45083630/… stackoverflow.com/questions/50189689/… etc. There is an issue because the
filter
method you used returns anArray
, butplaces
is defined as being a LazyCollectionArray– Larme
Nov 8 at 18:03
@Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
– kyo
Nov 8 at 18:11