将NSMutableArray转换为NSArray时发生错误

时间:2011-01-05 12:48:56

标签: nsmutablearray

我想将NSMutableArray转换为NSArray,它给出了错误,代码是

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);  
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

tempPeoples=[[NSMutableArray alloc]init];

for(int i=0;i<nPeople;i++){

    ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);
    [tempPeoples addObject:i1];

//[peoples addObject:i1];

}// end of the for loop

// Peoples is NSArray
// peoples=[[NSArray alloc] initWithArray: tempPeoples];

peoples=[NSArray arrayWithArray:tempPeoples];

请帮忙

1 个答案:

答案 0 :(得分:4)

我使用以下代码找到了解决方案

 ABAddressBookRef addressBook = ABAddressBookCreate();

NSArray *allPeople = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);  
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

tempPeoples= [NSMutableArray arrayWithCapacity:0];

for(int i=0;i<nPeople;i++){

    ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);

    NSString* name = (NSString *)ABRecordCopyValue(i1,kABPersonFirstNameProperty);

    [tempPeoples addObject:name];

//  [peoples addObject:i1];

}// end of the for loop

peoples=[NSArray arrayWithArray:tempPeoples];