Filtering Contacts Returns Mixed Contact Information - SwiftUISearchBar Sample Codelist comprehension vs. lambda + filterHow to design RESTful search/filtering?How do I call Objective-C code from Swift?#pragma mark in Swift?Swift Beta performance: sorting arraysPass Data to Container View SwiftUnable to filter tableview created using array of dictionarySwift 3/4: SearchBar not filtering results properly in TableViewSearch bar filter table view swift 4

Biological refrigeration?

A probably wrong proof of the Riemann Hypothesis, but where is the mistake?

A first "Hangman" game in Python

Is the Amazon rainforest the "world's lungs"?

Is this password scheme legit?

Many many thanks

Fan speed and power consumption

Defending Castle from Zombies

Why does matter stay collapsed in the core, following a supernova explosion?

Does the Reduce option from the Enlarge/Reduce spell cause a critical hit to do 2d4 less damage?

Finding square root without division and initial guess

Dual of a bimodule

Find most "academic" implementation of doubly linked list

How do we improve collaboration with problematic tester team?

Do sharpies or markers damage soft gear?

GDPR: What happens to deleted contacts re-entered through imports

Why does Windows store Wi-Fi passwords in a reversible format?

Is there any problem with a full installation on a USB drive?

Why is there not a willingness from the world to step in between Pakistan and India?

Did anybody find out it was Anakin who blew up the command center?

Notice period 60 days but I need to join in 45 days

Book featuring a child learning from a crowdsourced AI book

What stops you from using fixed income in developing countries?

Would Epic Heroism be an acceptable rule variant for a small, first-time group playing the Lost Mine of Phandelver adventure?



Filtering Contacts Returns Mixed Contact Information - Swift


UISearchBar Sample Codelist comprehension vs. lambda + filterHow to design RESTful search/filtering?How do I call Objective-C code from Swift?#pragma mark in Swift?Swift Beta performance: sorting arraysPass Data to Container View SwiftUnable to filter tableview created using array of dictionarySwift 3/4: SearchBar not filtering results properly in TableViewSearch bar filter table view swift 4






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am a new Swift developer. I have successfully pulled the contacts from the phone and saved them in an array of Contacts, each of which has this structure:



struct Contact {

var contactDetails:CNContact
var isFavorite:Bool


The array is called deviceContacts.



I also set up an empty array of Contacts called searchResults.



I have a search bar in the contacts table that I would like to automatically filter the contacts as you type. I am able to do this successfully with the following code for the Search Bar:



extension ContactsViewController: UISearchBarDelegate 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)

searchResults = deviceContacts.filter($0.contactDetails.familyName.prefix(searchText.count) == searchText)
searching = true
tableView.reloadData()

// This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.





Although this function will search as you type, and filter the results in realtime as you type more characters, the results are mixed up. They show the correct last names, but the wrong first names. The first names start at the top of the list. So, for example, if you search the standard contacts in the simulator for "Bell" you will get Bell, John. Bell is the second person in the list and John is the first person in the list.



I tried using the predicate search for contacts for this, but I could not get it to search as you type in real time like this function does.



Is there a way to pull the full contact when searching? Or a way to pull just a reference to the Contact that matches and put that Contact into the searchResults array? Or a way to use the predicate method to search as you type?



Any help would be appreciated.










share|improve this question






























    0















    I am a new Swift developer. I have successfully pulled the contacts from the phone and saved them in an array of Contacts, each of which has this structure:



    struct Contact {

    var contactDetails:CNContact
    var isFavorite:Bool


    The array is called deviceContacts.



    I also set up an empty array of Contacts called searchResults.



    I have a search bar in the contacts table that I would like to automatically filter the contacts as you type. I am able to do this successfully with the following code for the Search Bar:



    extension ContactsViewController: UISearchBarDelegate 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)

    searchResults = deviceContacts.filter($0.contactDetails.familyName.prefix(searchText.count) == searchText)
    searching = true
    tableView.reloadData()

    // This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.





    Although this function will search as you type, and filter the results in realtime as you type more characters, the results are mixed up. They show the correct last names, but the wrong first names. The first names start at the top of the list. So, for example, if you search the standard contacts in the simulator for "Bell" you will get Bell, John. Bell is the second person in the list and John is the first person in the list.



    I tried using the predicate search for contacts for this, but I could not get it to search as you type in real time like this function does.



    Is there a way to pull the full contact when searching? Or a way to pull just a reference to the Contact that matches and put that Contact into the searchResults array? Or a way to use the predicate method to search as you type?



    Any help would be appreciated.










    share|improve this question


























      0












      0








      0








      I am a new Swift developer. I have successfully pulled the contacts from the phone and saved them in an array of Contacts, each of which has this structure:



      struct Contact {

      var contactDetails:CNContact
      var isFavorite:Bool


      The array is called deviceContacts.



      I also set up an empty array of Contacts called searchResults.



      I have a search bar in the contacts table that I would like to automatically filter the contacts as you type. I am able to do this successfully with the following code for the Search Bar:



      extension ContactsViewController: UISearchBarDelegate 

      func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)

      searchResults = deviceContacts.filter($0.contactDetails.familyName.prefix(searchText.count) == searchText)
      searching = true
      tableView.reloadData()

      // This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.





      Although this function will search as you type, and filter the results in realtime as you type more characters, the results are mixed up. They show the correct last names, but the wrong first names. The first names start at the top of the list. So, for example, if you search the standard contacts in the simulator for "Bell" you will get Bell, John. Bell is the second person in the list and John is the first person in the list.



      I tried using the predicate search for contacts for this, but I could not get it to search as you type in real time like this function does.



      Is there a way to pull the full contact when searching? Or a way to pull just a reference to the Contact that matches and put that Contact into the searchResults array? Or a way to use the predicate method to search as you type?



      Any help would be appreciated.










      share|improve this question














      I am a new Swift developer. I have successfully pulled the contacts from the phone and saved them in an array of Contacts, each of which has this structure:



      struct Contact {

      var contactDetails:CNContact
      var isFavorite:Bool


      The array is called deviceContacts.



      I also set up an empty array of Contacts called searchResults.



      I have a search bar in the contacts table that I would like to automatically filter the contacts as you type. I am able to do this successfully with the following code for the Search Bar:



      extension ContactsViewController: UISearchBarDelegate 

      func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)

      searchResults = deviceContacts.filter($0.contactDetails.familyName.prefix(searchText.count) == searchText)
      searching = true
      tableView.reloadData()

      // This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.





      Although this function will search as you type, and filter the results in realtime as you type more characters, the results are mixed up. They show the correct last names, but the wrong first names. The first names start at the top of the list. So, for example, if you search the standard contacts in the simulator for "Bell" you will get Bell, John. Bell is the second person in the list and John is the first person in the list.



      I tried using the predicate search for contacts for this, but I could not get it to search as you type in real time like this function does.



      Is there a way to pull the full contact when searching? Or a way to pull just a reference to the Contact that matches and put that Contact into the searchResults array? Or a way to use the predicate method to search as you type?



      Any help would be appreciated.







      swift search filter contacts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 19 at 21:36









      TM LynchTM Lynch

      598 bronze badges




      598 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0















          In cellForRowAt I made a mistake and did not change the reference to the searchResults for the first name, only for the last name. Here is the code for the tableView:



          if searching 
          cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
          else
          cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName






          share|improve this answer


























            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',
            autoActivateHeartbeat: false,
            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%2f55250352%2ffiltering-contacts-returns-mixed-contact-information-swift%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            In cellForRowAt I made a mistake and did not change the reference to the searchResults for the first name, only for the last name. Here is the code for the tableView:



            if searching 
            cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
            else
            cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName






            share|improve this answer































              0















              In cellForRowAt I made a mistake and did not change the reference to the searchResults for the first name, only for the last name. Here is the code for the tableView:



              if searching 
              cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
              else
              cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName






              share|improve this answer





























                0














                0










                0









                In cellForRowAt I made a mistake and did not change the reference to the searchResults for the first name, only for the last name. Here is the code for the tableView:



                if searching 
                cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
                else
                cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName






                share|improve this answer















                In cellForRowAt I made a mistake and did not change the reference to the searchResults for the first name, only for the last name. Here is the code for the tableView:



                if searching 
                cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
                else
                cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 27 at 20:27









                Gary Kerr

                8,7662 gold badges36 silver badges45 bronze badges




                8,7662 gold badges36 silver badges45 bronze badges










                answered Mar 19 at 21:53









                TM LynchTM Lynch

                598 bronze badges




                598 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55250352%2ffiltering-contacts-returns-mixed-contact-information-swift%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

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해