问题
I'm wondering what is the best (fastest and simplest) way to have a textfield auto complete with iPhone's address book contacts information.
Would access the address book each time a key is pressed fast enough ? Or do I have to load everything in my own local database (CoreData typically) ? Another way would be to load everything in memory at each app launch. Any experience regarding the access speed of the adress book ?
ADDED: I'd like to avoid having to load a local copy of the address book content because it's not only more code to write but you also need to sync your local copy when address book is modified.
回答1:
For super fast character by character retrieval, the approach you need is called a Directed acyclic word graph.
回答2:
Would access the address book each time a key is pressed fast enough ?
Probably not for large books but test it an see. You could call ABAddressBookCopyPeopleWithName
with each additional character and see what pops up. You would most likely need to require a minimum number of characters e.g. 3 before performing the search because otherwise you would get a lot of returns for each the first character.
However, I don't think there are functions for phone numbers and addresses.
The alternative would be to create a tree data structure with letters and numbers that would branch out to a leaf containing an ABRecordID of a particular AddressBook entry. Using Core Data relationships is a quick and easy way to do that. Of course, you would have to update your tree continuously.
来源:https://stackoverflow.com/questions/5230976/best-way-to-implement-address-book-autocompletion-on-iphone