Blade
Blade

Reputation: 1437

Contact number stays nil

I have an mutableArray and put the number of a contact in it.

I use the following code:

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);


[thenumbers addObject:number];

In order to retrieve it, I do this:

Contacts *num = [thenumbers objectAtIndex:indexPath.row];
NSString *numbers = [NSString stringWithFormat:@"%@", [num number]];
cell.detailTextLabel.text = numbers;

I set breakpoints and it stops at the right lines. I also tried

NSLog(@"%@", number);

And it returns the numbers. And yes I have reloadDate in viewWillappear.

Upvotes: 0

Views: 410

Answers (2)

Abizern
Abizern

Reputation: 150665

You're releasing phoneNumbers straight after copying to it. Try moving it to after you use it.

Or, better still, transfer the ownership to the NSString object through ARC

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

And there is no need to call CFRelease(phoneNumbers) at all.

As for getting the numbers into the cell, you're getting confused about your types. You're putting an NSString into theNumber but your pulling out a Contacts. And then sending it some kind of number message.

You've put a string in the array, you can only pull out a string or a subclass.

NSString *numbers = [thenumbers objectAtIndex:indexPath.row];
cell.detailTextLabel.text = numbers;

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

In the following code,

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

You are releasing the phoneNumbers and then you are accesing the variable. Change it to as follows.

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
CFRelease(phoneNumbers);

Upvotes: 1

Related Questions