ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *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=[tempPeoples copy];
This code gives exception b/c I want to convert NSMutableArray to NSArray
Please Help
The subject reads, “How to convert
NSArraytoNSMutableArray“. To get anNSMutableArrayfrom anNSArray, use the class method onNSMutableArray+arrayWithArray:.Your code does not show the declaration for peoples. Assuming it’s declared as an
NSMutableArray, you can run into problems if you try to treat it as such. When you send the copy message to anNSMutableArray, you get an immutable object,NSArray, so if you try to add an object to a copiedNSMutableArray, you will get an error.CFArrayRefis toll free bridged toNSArray, so you could simplify your code this way:In the above code tempPeoples is an autoreleased
NSMutableArrayready for you to add or remove objects as needed.