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



enter image description here



//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 ?










share|improve this question



















  • 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











  • @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










  • @Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
    – kyo
    Nov 8 at 18:11














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



enter image description here



//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 ?










share|improve this question



















  • 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











  • @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










  • @Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
    – kyo
    Nov 8 at 18:11












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



enter image description here



//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 ?










share|improve this question















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



enter image description here



//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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:12

























asked Nov 8 at 17:23









kyo

17.3k37128226




17.3k37128226







  • 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











  • @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










  • @Larme : I wrapped it around an array and still get this : i.imgur.com/Rp4SzJr.png
    – kyo
    Nov 8 at 18:11












  • 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











  • @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










  • @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












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)





share|improve this answer




















  • 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

















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



enter image description here



Hope this will help someone like me in the future.






share|improve this answer



























    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 )





    share|improve this answer






















    • 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











    • 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










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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)





    share|improve this answer




















    • 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














    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)





    share|improve this answer




















    • 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












    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)





    share|improve this answer












    What also should work is if you are using NSPredicate



    let bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", searchText)
    places.filter(using: bPredicate)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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















    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












    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



    enter image description here



    Hope this will help someone like me in the future.






    share|improve this answer
























      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



      enter image description here



      Hope this will help someone like me in the future.






      share|improve this answer






















        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



        enter image description here



        Hope this will help someone like me in the future.






        share|improve this answer












        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



        enter image description here



        Hope this will help someone like me in the future.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 18:53









        kyo

        17.3k37128226




        17.3k37128226




















            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 )





            share|improve this answer






















            • 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











            • 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














            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 )





            share|improve this answer






















            • 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











            • 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












            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 )





            share|improve this answer














            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 )






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 18:53









            kyo

            17.3k37128226




            17.3k37128226










            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 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










            • 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










            • 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










            • 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

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo