Reputation: 833
I am accessing the address book on the iPhone and storing the phone numbers inside of an NSString object called numberValues. Originally these phone numbers are surrounded by a ton of extra junk like quotations, plus signs, parentheses, blank spaces etc. so I have all of these rangeOfString and stringByReplacingOccurencesOf method calls setup to get rid of all the extra code.
After deleting all of the extra junk, I am left with 10 digit phone numbers and that is all. So if a contact has 1 phone number, then my numberValues object should have a length property of 10. If a contact has 2 phone numbers, then my numberValues object should have a length property of 20.
Here's what's weird. When I NSLog numberValues.length on a contact with one phone number, it prints a length of 12 to the console. If I do the same thing on a contact with two phone numbers, it prints a length of 23 to the console.
I do not understand why the length property is printing with a bloated number when I have taken the time to delete all of the extra junk, including blank spaces, AND when I NSLog the numberValues object you can clearly see in the log that it contains 10 digit phone numbers and nothing more.
Here is how a contact with 1 phone number prints to the log when I do NSLog(@"%@", numberValues)
:
2014-01-19 10:36:54.912 PhotoTest1[2658:60b]
9495553119
And remember that when I do NSLog(@"%d", numberValues.length)
it will print out 12.
Here is how a contact with 2 phone numbers prints to the log when I do NSLog(@"%@", numberValues)
:
014-01-19 10:36:54.737 PhotoTest1[2658:60b]
7142973557
7149557735
And remember that when I do NSLog(@"%d", numberValues.length)
it will print out 23.
Any ideas why the length properties are longer than they should be?
Thanks for the help.
EDIT: Here is all of my code starting right after I successfully gain access to the user's address book and then all the way down to where I start accessing the length property of the numberValues object:
int z = allContacts.count - 1;
for (int i = 0; i < z; i++)
{
NSArray *allContacts = (__bridge_transfer NSArray
*)ABAddressBookCopyArrayOfAllPeople(m_addressbook);
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
ABMultiValueRef *phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
NSMutableArray *numbers = [NSMutableArray array];
//NSLog(@"The count: %ld", ABMultiValueGetCount(phoneNumber));
NSString *number = (__bridge_transfer NSString*)ABMultiValueCopyArrayOfAllValues(phoneNumber);
[numbers addObject:number];
NSString *numberValues = [[NSString alloc]initWithFormat:@"%@", number];
NSLog(@"Here are the numbers for the contact named %@: %@", firstName, numberValues);
if([numberValues rangeOfString:@"+"].location == NSNotFound) {
NSLog(@"Phone Number does not contain a plus sign.");
} else {
numberValues = [numberValues stringByReplacingOccurrencesOfString:@"+1" withString:@""];
NSLog(@"This new number value should not have a + anymore: %@", numberValues);
}
if([numberValues rangeOfString:@" ("].location == NSNotFound) {
NSLog(@"No phone numbers contain blank space with start of parentheses");
} else {
NSLog(@"Phone number(s) do contain blank space with start of parentheses");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@" (" withString:@""];
NSLog(@"The new number values should not contain a blank space with the start of parentheses: %@", numberValues);
}
if([numberValues rangeOfString:@") "].location == NSNotFound) {
NSLog(@"Phone number(s) do not contain ) and a blank space");
} else {
NSLog(@"Phone numbers do contain ) and a blank space");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@") " withString:@""];
NSLog(@"These new phone number values should not have ) with a blank space after it: %@", numberValues);
}
if([numberValues rangeOfString:@"-"].location == NSNotFound) {
NSLog(@"No numbers contain a dash");
} else {
NSLog(@"Number(s) does contain a dash");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"The new number values should not have any dashes: %@", numberValues);
}
if([numberValues rangeOfString:@"("].location == NSNotFound) {
NSLog(@"Number(s) do not contain a (");
} else {
NSLog(@"Number(s) contains a (");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@"(" withString:@""];
NSLog(@"The new number(s) should not have any ( in them: %@", numberValues);
}
if ([numberValues rangeOfString:@"\""].location == NSNotFound) {
NSLog(@"Number does not contain any quotations");
} else {
NSLog(@"Number(s) do contain quotations");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"Numbers should not have any quotations: %@", numberValues);
}
if([numberValues rangeOfString:@")"].location == NSNotFound) {
NSLog(@"The final ) has been deleted");
} else {
NSLog(@"Need to delete the final )");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@")" withString:@""];
NSLog(@"Final value(s) should not have a trailing ) at the end: %@", numberValues);
}
if([numberValues rangeOfString:@","].location == NSNotFound) {
NSLog(@"The final , has been deleted");
} else {
NSLog(@"Need to delete the final ,");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@"," withString:@""];
NSLog(@"%@", numberValues);
NSLog(@"Final value(s) should not have a trailing , at the end: %lu", (unsigned long)numberValues.length);
}
if([numberValues rangeOfString:@" "].location == NSNotFound) {
NSLog(@"No blank spaces.");
} else {
NSLog(@"There are blank spaces that need to be deleted");
numberValues = [numberValues stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", numberValues);
}
NSLog(@"RIGHT BEFORE THE NEW IF STATEMENT");
NSLog(@"%d", numberValues.length);
if(numberValues.length < 11) {
NSLog(@"RUNNING PFQUERY");
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"username" equalTo:numberValues];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Here are the objects that have been returned from Parse: %@", objects);
} else {
NSLog(@"CHARACTERS: %d", numberValues.length);
int howManyNumbers = numberValues.length / 10;
NSLog(@"%d", howManyNumbers);
}
EDIT 2 FOR VEDDERMATIC'S COMMENT:
I just did NSLog(@"###%@###", numberValues);
and this is how it prints to the log:
2014-01-19 11:06:00.529 PhotoTest1[2678:60b] ###
3097679973
###
Upvotes: 0
Views: 190
Reputation: 33359
The length is correct, there are more characters that you are not stripping. There are over a million characters supported by NSString and you can't check for all of them.
Instead you should use RegEx to delete everything that is not a number.
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9]" options:0 error:nil];
NSString *phoneNumber = [regex stringByReplacingMatchesInString:phoneNumber options:0 range:NSMakeRange(0, phoneNumber.length) withTemplate:@""];
Upvotes: 2