Xcodeで連絡先アプリを実行しようとしていますが、連絡先リストが表示されますが、名前をクリックするとエラーEXC_BAD_ACCESSがスローされます.NSLogで確認したところ、テーブルビュー自体にデータを入力しているときに配列にエラーが発生することがわかりました.これは私のコードです:
// として初期化された配列
@property (strong,nonatomic) NSMutableArray *filteredData,*contactAdd;
//この関数に設定された配列
-(void)reloadAddressBook
{
self.contactAdd = [[NSMutableArray alloc]init];
ABAddressBookRef addressBook = ABAddressBookCreate();//ABAddressBookCreateWithOptions(NULL,NULL);
if(ABAddressBookHasUnsavedChanges(addressBook))
{
ABAddressBookSave(addressBook,NULL);
}
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i < nPeople; i++ )
{
ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
[self.contactAdd addObject:(__bridge id)(person)];
NSLog(@"@details %@",contactAdd);
CFRelease(person);
}
CFRelease(addressBook);
}
//この関数のエラー
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if(isFiltered)
{
ABRecordRef person = (__bridge ABRecordRef)[self.filteredData objectAtIndex:indexPath.row];
NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[cell.textLabel setText:tweet];
CFRelease(person);
}
else
{
ABRecordRef person = (__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);
NSLog(@"%@",person);
NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[cell.textLabel setText:tweet];
CFRelease(person);
NSLog(@"%@",indexPath);
// I have 4 contacts currently and error occurs here on 4th time and when I continue error occurs on 2nd time
NSLog(@"@details %@",contactAdd);
}
return cell;
}
//実行時にここでエラーを取得
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ABPersonViewController *currentPersonView=[[ABPersonViewController alloc]init];
ABRecordRef person;
if(isFiltered)
person=(__bridge ABRecordRef)([self.filteredData objectAtIndex:indexPath.row]);
else
person=(__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);//error here
currentPersonView.displayedPerson=person;
currentPersonView.allowsEditing=YES;
currentPersonView.allowsActions=YES;
[self.navigationController pushViewController:currentPersonView animated:YES];
}
//NSLog
2013-08-01 13:11:12.715 連絡先[7722:207] 2 つのインデックス [0, 0]
現在の言語: auto; 現在、objective-c 2013-08-01 13:11:26.614 Contacts[7722:207] @details ( "CPRecord: 0xba1b340 ABPerson", "CPRecord: 0xba1c870 ABPerson", "CPRecord: 0xba1c160 ABPerson", "CPRecord: 0xba1c990 ABPerson" )
2013-08-01 13:11:26.616 連絡先[7722:207]
2013-08-01 13:11:26.617 連絡先[7722:207] 2 つのインデックス [0、1]
2013-08-01 13:11:27.566 連絡先[7722:207] @詳細 (「CPRecord: 0xba1b340 ABPerson」、「CPRecord: 0xba1c870 ABPerson」、「CPRecord: 0xba1c160 ABPerson」、「CPRecord: 0xba1c990 ABPerson」) 2013-08 -01 13:11:27.567 連絡先[7722:207] 2013-08-01 13:11:27.568 連絡先[7722:207] 2 つのインデックス [0、2] (gdb)