views:

107

answers:

3

The problem is that I always get runtime crashes at any method invoked at phones variable. At this version I get an error at 1 (ABMultiValueCopyValueAtIndex). If I'll comment this line, the code crashes at 2 (ABMultiValueGetCount). It looks like the property was empty. If I NSLog the phones variable. I get (null). I test the code on iPhone Simulator, I have some dummy contacts there with some phone numbers. Also firstName and lastName work like a charm.

for(id person in people){
    NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, 0); /*1*/
/*2/  id ph, phLb;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {  
        phLb = ABMultiValueCopyLabelAtIndex(phones, i);
        ph = ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"%@,%@", phLb, ph);
        CFRelease(phLb);
        CFRelease(ph);
    }
*/  
NSLog(@"%@", firstName);
NSLog(@"%@", lastName);
NSLog(@"%@", phones);
NSLog(@"%@", phone);

[firstName release];
[lastName release];
[phone release];
[phones release];

}

A: 

Learn to use a debugger. Also, your code leaks.

tc.
I know that it leaks. I would first like to get it to work, then retain all the variables.
Alistra
If you don't know anything about AddressBook, then don't answer the question, tc. Thanks!
Jasconius
A: 

I think your assumptions about how to procure a ABMultiValueRef are wrong. I don't have my AB code handy at this computer, but verify that ABRecordCopyValue is the appropriate method. And then do research on ABMultiValueRef on how to access its contents. It's a whole different object set than simple strings.

Jasconius
As you see in the commented part. I tried with the id type and that didn't work either. I think that example is nearly copy-pasted from the apple documentation, that how to use multiRef part. I'll check tommorow at work.
Alistra
A: 

The code:

addressBook = ABAddressBookCreate();
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);

deallocated the addressbook and all the objects that were reffered from it was released too. So the pointers to the MultiValueRefs were deallocated before use.

Alistra