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;
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
add a comment |
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
add a comment |
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
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
swift search filter contacts
asked Mar 19 at 21:36
TM LynchTM Lynch
598 bronze badges
598 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
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%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
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
add a comment |
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
add a comment |
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
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
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
add a comment |
add a comment |
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.
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.
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%2f55250352%2ffiltering-contacts-returns-mixed-contact-information-swift%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