24
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

1

3 Answers 3

44

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.

Sign up to request clarification or add additional context in comments.

Comments

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];

2 Comments

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,
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'?
4

Simply you can do that

NSArray *yourArray ; // Static Array
NSMutableArray* subArrayData = [yourArray mutableCopy];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.