23
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

Machavity
  • 30,841
  • 27
  • 92
  • 100
Ali
  • 10,774
  • 10
  • 56
  • 83
  • 2
    Answered here http://stackoverflow.com/questions/3222122/converting-nsarray-to-nsmutablearray – Mantar Dec 31 '10 at 10:17

3 Answers3

43

The subject reads, "How to convert NSArray to NSMutableArray". To get an NSMutableArray from an NSArray, use the class method on NSMutableArray +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 an NSMutableArray, you get an immutable object, NSArray, so if you try to add an object to a copied NSMutableArray, you will get an error.

CFArrayRef is toll free bridged to NSArray, so you could simplify your code this way:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
//NSMutableArray *tempPeoples = [NSMutableArray arrayWithArray:(NSArray*)allPeople];
// even better use the NSMutableCopying protocol on NSArray
NSMutableArray *tempPeoples = [(NSArray*)allPeople mutableCopy];
CFRelease(allPeople);
return tempPeoples; // or whatever is appropriate to your code

In the above code tempPeoples is an autoreleased NSMutableArray ready for you to add or remove objects as needed.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
sean woodward
  • 1,750
  • 1
  • 24
  • 36
4

This code gives exception b/c I want to convert NSMutableArray to NSArray

This is very unlikely. NSMutableArray is a derived class of NSArray, so copying in that direction isn't an issue.

Maybe you've got an error because you don't retain the array. arrayWithArray returns an autorelease object. Either use [tempPeoples copy] or [[NSArray alloc] initWithArray: tempPeoples];

jv42
  • 8,521
  • 5
  • 40
  • 64
  • I did ,but it is again giving following exception:2010-12-31 15:30:24.889 Appointment[2708:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType isEqualToString:]: unrecognized selector sent to instance 0x420f900' 2010-12-31 15:30:24.890 Appointment[2708:207] Stack: ( 30393435, 2510714121, 30775355, 30344822, 30197442, 4347985, 9508, 3018252, – Ali Dec 31 '10 at 10:31
  • Well, I don't see the line causing the exception in the code you've shown, I can't help you there. Where do you call 'isEqualToString'? – jv42 Dec 31 '10 at 11:42
3

Simply you can do that

NSArray *yourArray ; // Static Array
NSMutableArray* subArrayData = [yourArray mutableCopy];
Mehedi Hasan
  • 359
  • 3
  • 10