How to append two NSMutableArray's in Iphone sdk or append an NSArray With NSMutableArray? - objective-c

I need to append two NSMUtableArray's can any one suggest me how it possible?
My code is:
NSMutableArray *array1 = [appDelegate getTextList:1];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];//I am getting exception here.
Anyone's help will be much appreciated.
Thanks all,
Lakshmi.

What's probably happening, is that your [appDelegate getTestList:1] is not actually returning a NSMutableArray, but a NSArray. Just typecasting the array as mutable by holding a pointer to it like that will not work in that case, instead use:
NSMutableArray *array1 = [[appDelegate getTextList:1] mutableCopy];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];
Or you could store the 'textList' variable that you have in your appDelegate as an NSMutableArray in the first place. I am assuming that you have an NSArray of NSArrays (or their mutable versions). Eg.
// In the class interface
NSMutableArray *textLists;
// In the function in which you add lists to the array
NSMutableArray *newTextList;
[self populateArray:newTextList]; // Or something like that
[textLists addObject:newTextList];
Note: that you will probably have a different workflow, but I hope that you get the idea of storing the actual lists as NSMutableArrays.
Another Note: the second method WILL modify in place the NSMutableArray that [appDelegate getTextList:1]; returns

Try this:
NSMutableArray *result =
[[appDelegate getTextList:1] mutableCopy]
addObjectsFromArray:[appDelegate getTextList:2]];
You're getting the exception because you're trying to send mutating messages to an immutable array.

Related

How to copy NSArray to another NSArray?

I have many different NSArrays, and according to the users choice I want one of them to be copied to a new NSArray. How do I copy one NSArray to another?
There can be several ways for this-
array1 = [array2 copy];
Use initWithArray method.
You can also use initWithArray:copyItems: method. (This if for NSMutableArray)
you can use the
NSArray *_newArray = [NSArray arrayWithArray:_oldArray];
or if you prefer better, you can use:
NSArray *_newArray = [[NSArray alloc] initWithArray:_oldArray];
(in that case the object of the first array won't be copied, that get only a retain front he second NSArray, you can remove the object from any array it won't affect the other array, but if you change any object in any NSArray it will be changed in the other one as well because there is both of the old and the new array is working with the same instance of the objects.)
if your plan is to make another instance of the old objects in the new array:
NSArray *_newArray = [[NSArray alloc] initWithArray:_oldArray copyItems:true];
if you are using the ARC, you won't need to do anything else, if you are not, in the case of both -initWithArray: or -initWithArray:copyItems: you should use the [_newArray release]; to release the array after you don't want to use anymore.
As well as
NSArray *newArray = [oldArray copy];
if you need to add/remove from the new array, the simplest way to make a mutable copy is:
NSMutableArray *editableArray = [oldArray mutableCopy];
The above functions both make shallow copies, for deep copy it's as #holex and #rishi mentioned
NSArray *newArray = [[NSArray alloc] initWithArray:oldArray copyItems:true];
NSMutableArray *editableArray = [[NSMutableArray alloc] initWithArray:oldArray copyItems:true];

NSSet to NSArray casting calling objectAtIndex?

I'm trying to update an MKMapView by removing all annotations outside the visible area, and adding and removing some annotations inside the visible area. This is my code:
NSSet *visibleAnnotations = [mapView annotationsInMapRect:[mapView visibleMapRect]];
NSSet *allAnnotations = [NSSet setWithArray:[mapView annotations]];
NSMutableSet *nonVisibleAnnotations = [NSMutableSet setWithSet:allAnnotations];
[nonVisibleAnnotations minusSet:visibleAnnotations];
[mapView removeAnnotations:(NSArray *)nonVisibleAnnotations];
NSMutableSet *newAnnotations = [NSMutableSet setWithArray:[_zoomLevels objectAtIndex:clusterLevel]];
[newAnnotations minusSet:visibleAnnotations];
[mapView addAnnotations:(NSArray *)newAnnotations];
This gives me the error -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x13cd40 after the final line in which I cast newAnnotations to an NSArray then add the annotations. Is there something about casting an array to a set that causes this? If so, is there a way round it?
Despite you're casting NSMutableSet to NSArray, that simple casting won't make NSSet class respond to NSArray's messages. You have to fill an actual NSArray with the elements of the NSSet like this:
NSArray *array = [theNsSet allObjects];
Casting an NSSet object to NSArray will not do anything else that tricking the compiler into thinking that the object is an NSArray. Actually, the object is an NSSet object and trying to use it as an NSArray will produce failure.
Another way to see it is that casting is just a trick on pointers, not on the pointed-to objects, that remain unaltered.
Casting is only safe in certain cases, like when you cast from a derived class to a base class; or when you are absolutely sure that the underlying object real type is consistent with the type you are casting it to.
Anyway, in your specific case, you might try to access the NSSet elements through an NSArray by using:
[newAnnotations allObjects]
This
Returns an array containing the set’s members, or an empty array if the set has no members.
Another way to get an NSMutableArray from an NSSet is
NSMutableArray * array= [[set allObjects] mutableCopy];
Also, the solution proposed by satzkmr gives you an "Incompatible pointer" warning. (Sorry to write this here, I don't have enough reputation to comment).
Yes, you should first store the set into an array like this...
NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];
Following Step to be followed to convert a NSSet into NSArray or NSMutableArray,
NSSet *airports = [NSSet setWithObjects:#"Chennai",#"Mumbai",#"Delhi", nil];
NSLog(#"Set Elemets Before Move:%#", airports);
NSMutableArray *movedAirports = [airports allObjects];
NSLog(#"Array Elements After Moving from Set:%#", movedAirports);
NSArray *array = [sets allObjets];

Is this a correct declaration of an Array in Objective c?

NSArray *arr=[[[NSArray alloc]autorelease]autorelease];
Close, but no cigar.
If you want an autoreleased NSArray, you'd need to use:
NSArray *arr = [[NSArray alloc] init] autorelease];
That said, this will simply get you an empty immutable array, so you'll most likely want to populate it via one of the initWithObjects: style methods. (See the full NSArray class reference for more information.)
Or you can just declare it like this:
[NSArray array];
This gives you an autoreleased instance of the array.

Handling NSArray and NSMutableArray

I'm working with the MKMapView and I'm using some arrays to handle the title of points on the map
NSString *mapTitles = #"title1^^title2^^title3^^title4";//this data changes between views
NSArray * titlesArray = [mapTitles componentsSeparatedByString: #"^^"];
NSMutableArray * maptitle = [NSMutableArray arrayWithCapacity:[titlesArray count]];
[maptitle addObjectsFromArray:titlesArray];
When a user navigates to another page I want to clear the NSMutableArray so that when they come back to the map I can refresh it with new data. However as the NSMutableArray is getting populated by an NSArray which I can't clear, how do I ensure that the NSMutableArray only gets populated by new data as opposed to something that the NSArray may have kept from the previous view?.
Is it simply a case of releasing the NSArray, for example in viewWillDisappear?
Thanks
You can still clear the mutable array itself:
[mapTitle removeAllObjects];
You shouldn't release the titlesArray because it's autoreleased.

How to convert NSArray to NSMutableArray

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 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.
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];
Simply you can do that
NSArray *yourArray ; // Static Array
NSMutableArray* subArrayData = [yourArray mutableCopy];