0

I have a method for getting all the contacts from the iPhone contacts app and i want to add the phone numbers to an object after i have removed all the spaces in the phone number string. The problem is that this only works for some of the contacts. I have noticed that in the debugger the string-objects sometimes show in a blue color and sometimes in black. Anybody have a clue what is going on here?

Images:

Does not remove spaces in phone number

http://ctrlv.in/293692

Removes spaces in phone number

http://ctrlv.in/293691

Code:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef sortedPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

//RETRIEVING THE FIRST NAME AND PHONE NUMBER FROM THE ADDRESS BOOK

CFIndex number = CFArrayGetCount(sortedPeople);

NSString *firstName;
NSString *phoneNumberFromContact;

for(int i = 0; i < number; i++)
{

    ABRecordRef person = CFArrayGetValueAtIndex(sortedPeople, i);
    firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    phoneNumberFromContact = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, 0);

    if(phoneNumberFromContact != NULL)
    {
        Contact *contact = [[Contact alloc]init];

        contact.firstName = firstName;
        phoneNumberFromContact = [phoneNumberFromContact stringByReplacingOccurrencesOfString:@" " withString:@""];
        contact.phoneNumber = phoneNumberFromContact;
        [self.contacts addObject:contact];
    }
}
  • 2
    The black vs blue is modified in the last step vs not modified in the last step. If you step over/into/out the modified values will show up in blue. IF it didn't change it will remain black. – Putz1103 Feb 10 '14 at 14:41
  • Ok, thanks. But the real problem is that the method for removing the spaces doesn't work on some of the NSStrings. Do you know anything about this? – Sean Inge Eide-Asbjørnsen Feb 10 '14 at 14:47
  • Are you sure they are separated by spaces, (not a different character that looks like spaces)? – Putz1103 Feb 10 '14 at 15:11
  • No,but why would some phone numbers be saved with spaces between and some not in the iPhone contacts app? Any thoughts? – Sean Inge Eide-Asbjørnsen Feb 10 '14 at 15:13
  • No idea. Saved with different version of the contacts app, app automatically adding spaces vs user adding spaces, imported vs manual entry contacts. Could be anything, or they could all be spaces and your code is doing something funny. I would NSLog the before and after phone number strings and see what the difference is with the strings that work and those that don't. – Putz1103 Feb 10 '14 at 15:16
  • Used the code from this thread and that solved the problem. Seems like they were not all separated by spaces. Thanks for your help. http://stackoverflow.com/questions/1129521/remove-all-but-numbers-from-nsstring – Sean Inge Eide-Asbjørnsen Feb 10 '14 at 15:26

1 Answers1

1

There are many characters that appear as spaces in NSStrings. Removing all instances of all of them is rather difficult. The best method for your case is to keep only the characters you want (numbers). As the answer you referenced states you need to create a set of characters to keep:

NSCharacterSet *numbers = [NSCharacterSet 
        characterSetWithCharactersInString:@"0123456789"];

Then you need to remove everything but those characters. This can be done in a few different ways. The scanner in the answer you suggested is likely the fastest. But the way with the fewest lines of code (and in my mind most readable) would be something like:

number = [[number componentsSeparatedByCharactersInSet: numbers] componentsJoinedByString: @""];

It may be slow, so if you do this a million times then keep to the scanner.

Putz1103
  • 6,211
  • 1
  • 18
  • 25