0

I am accessing the address book contacts, and then running a for loop on all of the contacts. The loop's main purpose is to access the First Name and Phone Number values of the user's address book contacts and place those values into NSString objects.

All of the above works perfectly. My problem is that I need to execute specific code if the phone number data contains this string: _$!<Mobile>!$_

So I setup a rangeOfString method call on the object that contains the phone number data and this is causing problems.

Here is what prints to the console once I added the rangeOfString method call to my code:

2014-01-18 12:10:49.190 PhotoTest1[1244:60b] -[__NSCFType rangeOfString:]: unrecognized     selector sent to instance 0x14e9ac90
2014-01-18 12:10:49.193 PhotoTest1[1244:60b] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '-[__NSCFType rangeOfString:]: unrecognized selector    sent to instance 0x14e9ac90'
*** First throw call stack:
(0x2f8f6f4b 0x39d376af 0x2f8fa8e7 0x2f8f91cb 0x2f8fa478 0x55e29 0x3206e37b 0x3206e139     0x32117e27 0x3215449b 0x32152dd3 0x32151e25 0x3232dcb3 0x3209e713 0x3209e6b3 0x3209e691    0x3208a11f 0x3209e107 0x3209ddd9 0x32098e65 0x3206e79d 0x3206cfa3 0x2f8c2183 0x2f8c1653 0x2f8bfe47 0x2f82ac27 0x2f82aa0b 0x34551283 0x320ce049 0x54495 0x3a23fab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

And here is all of my code starting with the for loop. The error prints to the console as soon as it gets to "if([phoneNumber rangeOfString:@"+"].location == NSNotFound)":

for (i = 0; i < [allContacts count]; i++)
    {


        Person *person = [[Person alloc] init];

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
        NSString *firstName = (__bridge_transfer NSString
                                *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *phoneNumber = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);


        person.firstName = firstName;

        person.phoneNumber = phoneNumber;

        NSLog(@"%@", phoneNumber);

        //MOBILE PHONE PSEUDOCODE STARTS HERE

        // If the value of phoneNumber contains the text “_$!<Mobile>!$_” , then we want to grab a substring out of it.

        if([phoneNumber rangeOfString:@"_$!<Mobile>!$_"].location == NSNotFound) {

            NSLog(@"Not a mobile phone number");

        } else {

            NSLog(@"It is a mobile phone number");

            // If the value of phoneNumber contains a “+”, then grab the substring that starts with the + and then the next additional 10 spaces and place the substring in the mobilePhoneNumber object. (Which will grab the + sign and the 10 digit phone number. )

            if([phoneNumber rangeOfString:@"+"].location == NSNotFound) {


                NSLog(@"Phone number does not start with a +");


            } else {

                NSLog(@"Phone number does start with a +");

                NSString *subString = [phoneNumber substringWithRange: NSMakeRange(0, [phoneNumber rangeOfString: @"+"].location)];

                NSLog(@"%@", subString);

            }

        }

        //PFQUERY CODE TESTING!!!

}   
}

}

Thanks for the help.

user3117509
  • 833
  • 3
  • 14
  • 32

1 Answers1

3

The phone Number is no string . it is defined as a multival as there can be 0..x numbers there.

you can't just treat it as a string . you need to enumerate through the multival\

e.g. to get the first of ANY kind

    ABMultiValueRef phoneEntries = ABRecordCopyValue(person, kABPersonPhoneProperty);       
    NSMutableArray *numbers = [NSMutableArray array)];
    for(int i = 0; i < ABMultiValueGetCount(phoneEntries); i++) {
         NSString *number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneEntries, i))];
         // Do what you want with the number
         [numbers addObject:number];
    }
    CFRelease(phoneEntries);
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Gotcha that makes sense. My main goal is to extract the mobile phone number data and get rid of all the other crap that is stored. Can I do this by enumerating through the multival as you said, and if so can you give me a code example or point me to some documentation? – user3117509 Jan 18 '14 at 20:45
  • So if i understand this correctly... by using your code above, I will be able to do exactly what I was trying to do before AND do it by using rangeOfString BECAUSE you now have me using ABMultiValueGetCount and ABMultiValueCopyValueAtIndex instead of using count and ABRecordCopyValue? – user3117509 Jan 18 '14 at 20:54
  • I still don't understand WHY we cannot use rangeOfString with my original code when I was instantiating an NSString object and able to place the data in there. If I can place the data in an NSString in the first place then why can't I use rangeOfString? What's the difference between this situation and all of the other times I've used rangeOfString on an NSString object? – user3117509 Jan 18 '14 at 21:06
  • I think it is a string because of this statement: NSString *phoneNumber = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty); – user3117509 Jan 18 '14 at 21:07
  • only because you say it should be. think of the pointer type as merely a label. – Daij-Djan Jan 18 '14 at 21:07
  • @user3117509 no sorry it was a ABMultiValueRef all along – Daij-Djan Jan 18 '14 at 21:08
  • See above, aren't we placing the data inside of an NSString object called phonenumber? And if yes, then we should be able to use rangeOfString right? I am not trying to argue with you, just trying to understand the theory behind all of this. – user3117509 Jan 18 '14 at 21:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45553/discussion-between-daij-djan-and-user3117509) – Daij-Djan Jan 18 '14 at 21:08
  • Objective-c is the first programming language I have ever learned and I started learning about 45 days ago and have spent 145 hours learning and coding. Am I doing alright based on how long I've been learning for? Would appreciate your input since you you have such a high SO rank. – user3117509 Jan 18 '14 at 21:27
  • I really don't know BUT I can say that not getting pointers is an issue 'all' students here struggle with. It is fundamental though (IMO) -- don't forget objC is just pimped C – Daij-Djan Jan 18 '14 at 21:31
  • Ok thank you. I will try and brush up on my pointer theory. Also, it may be because this is the first time i have worked with Core Foundation so I'll brush up on that as well. Thank you I appreciate you taking the extra time to help cheers. – user3117509 Jan 18 '14 at 21:37