I am having three array variables from different array list , how to add them and place them in a single array list.i.e suppose if abc is from array list 1,pqr from array list2 and xyz from array list3 , after adding into new list arraylist 4 should have abc,pqr,xyz
If I understand your question correctly, just do:
NSMutableArray *newArray = [NSMutableArray array];
[newArray addObjectsFromArray:array1];
[newArray addObjectsFromArray:array2];
[newArray addObjectsFromArray:array3];
Use the below method of NSMutableArray.
- (void)addObjectsFromArray:(NSArray *)otherArray
otherArray : An array of objects to add to the end of the receiving array’s content.
See in Apple Documentation.
I assume list1,list2,list3 is either the type of NSArray OR NSMutableArray.
NSMutableArray *myArray = [NSMutableArray alloc] init];
[myArray addObjectsFromArray:list1];
[myArray addObjectsFromArray:list2];
[myArray addObjectsFromArray:list3];
Related
Ok so I populate the array like this:
NSMutableArray *participants;
for(int i = 0; i < sizeofpm; i++){
NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i];
NSString *pmpart_email = [pmpart_dict objectForKey:#"email"];
NSString *pmpart_email_extra = [#"pm" stringByAppendingString:pmpart_email];
[participants setValue:pmpart_email forKey:pmpart_email_extra];
NSLog(#"%#", participants);
}
sizeofpm is 1. that is using count. to get the number of values in the array. How can i store values to that array? It doesnt seem to be working. Thanks!
you need to alloc it first. Try to change the first line to:
NSMutableArray* participants = [[NSMutableArray alloc] init];
also using setValue:forKey: wont work with an NSMutableArray as an array has no key.
Try using [participants addObject:pmpart_email];.
You don't create the array, you just declare it.
NSMutableArray *participants = [NSMutableArray array];
After that, setValue:forKey: will not add objects to an array. You need addObject::
[participants addObject:pmpart_email];
There is no key.
You are assigning a value to an NSMutableArray *participants like how you assign values to an NSDictionary object. To assign values to NSMutableArray you can call - (void)addObject:(id)anObject
So, I as some of the other answer have stated, you're missing your initializer for participants. However, judging by your use of setValue:forKey:, and how you appear to be structuring your data, you're not looking for NSMutableArray, but instead NSMutableDictionary. Arrays are simply lists, whereas dictionaries maintain key-value relationships, which you appear to be attempting to leverage.
Try this:
// some classes provide shorthand for `alloc/init`, such as `dictionary`
NSMutableDictionary *participants = [NSMutableDictionary dictionary];
for(int i = 0; i < sizeofpm; i++){
NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i];
NSString *pmpart_email = [pmpart_dict objectForKey:#"email"];
NSString *pmpart_email_extra = [#"pm" stringByAppendingString:pmpart_email];
[participants setValue:pmpart_email forKey:pmpart_email_extra];
NSLog(#"%#", participants);
}
This will give you a dictionary in the form of
{
pmpart_email_extra: pmpart_email
}
i have searched a lot but not able two sort my array a/c to requirement
i used this code:
[array1 sortArrayUsingSelector:#selector(caseInsensitiveCompare:) withPairedMutableArrays:arrForName, arrForAddress, nil];
thanks
On the NSArray Class Reference there isn't a - sortArrayUsingSelector:withPairedMutableArrays: method. Neither on the NSMutableArray Class Reference. If you want, you can use other methods like the NSMutableArray sortUsingSelector: method.
Put the two arrays into a dictionary as keys and values
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:arrForAddress forKeys:arrForName];
// Sort the first array
NSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:#selector(compare:)];
// Sort the second array based on the sorted first array
arrForAddress=[[NSMutableArray alloc]init ];
NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
// arrForAddress = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[arrForAddress addObjectsFromArray:sortedSecondArray];
NSLog(#"arrangesort......%#",arrForAddress);
I'm Trying here to add many arrays to one NSMutableArray
Actually am adding the same array with different values Many Times to one NSMutable Array
this the code:
NSMutableArray* wordsArray =[[NSMutableArray alloc] init ];
NSMutableArray* meaningsArray=[[NSMutableArray alloc]init];
NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init];
NSMutableArray* meaningsArrayTemp=[[NSMutableArray alloc]init ];
NSMutableArray* allWords =[[NSMutableArray alloc]initWithCapacity:2];
NSMutableArray* allMeanings=[[NSMutableArray alloc]initWithCapacity:2];
for(int i=0;i<2;i++)
{
int wordsCounter=0;
[wordsArrayTemp removeAllObjects];
[meaningsArrayTemp removeAllObjects];
for(NSString *tmp in wordsArray)
{
NSString *meaning =[meaningsArray objectAtIndex:wordsCounter++];
subtmp= [ tmp substringWithRange:NSMakeRange(0,1)];
if([currentTable isEqualToString:#"arabicToEnglish"])
{
if([subtmp isEqualToString:[arabicLetters objectAtIndex:i]])
{
[wordsArrayTemp addObject:tmp];
[meaningsArrayTemp addObject:meaning];
}
}
else
if([subtmp isEqualToString:[englishLetters objectAtIndex:i]])
{
[wordsArrayTemp addObject:tmp];
[meaningsArrayTemp addObject:meaning];
}
}
[allWords insertObject:wordsArrayTemp atIndex:i];
// [allWords addObject: wordsArrayTemp];
[allMeanings addObject:meaningsArrayTemp];
NSLog(#"all words count%i",[[allWords objectAtIndex:i] count]);
}
The Problem :
The supposed behavior here is to have 2 different values in the allWords array .
But what Actually happens is that the 2 values filled with the same value with the last index Value.
I mean [allWords objectAtIndex:0] should have 2000 object and [allWords objectAtIndex:1] should have 3000 ,but what happens that they both have 3000 object !!
what am I missing here?!!
thnx
when you add an object to an array the object is not copied. You just save its memory address.
Basically you added the same temporary array to the parent array. And you did all your array manipulations to the same array.
Maybe this piece of unrolled loop code will make it a little bit clearer.
// create new array on a specific memory address. let's say this address is 0x01
NSMutableArray* wordsArrayTemp=[[NSMutableArray alloc]init];
// first iteration of your loop
// remove all objects from array at memory address 0x01
[wordsArrayTemp removeAllObjects];
// add objects to the array at address 0x01
[wordsArrayTemp addObject:tmp];
// insert array (still at address 0x01) to the parent array
[allWords insertObject:wordsArrayTemp atIndex:i];
// your allWords array now looks like this: {array#0x01}
// second iteration of your loop
// remove all objects from array at memory address 0x01!!! (still the same array as in the first iteration)
// since it's the same array all objects from the array at [allWords objectAtIndex:0] are removed too
[wordsArrayTemp removeAllObjects];
// add objects to the array at address 0x01
[wordsArrayTemp addObject:tmp];
// insert array (still at address 0x01) to the parent array
[allWords insertObject:wordsArrayTemp atIndex:i];
// your allWords array now looks like this {array#0x01, array#0x01}
the solution is pretty easy.
At the beginning of the for-loop instead of removing allObjects from the array create new arrays.
Just replace
[wordsArrayTemp removeAllObjects];
[meaningsArrayTemp removeAllObjects];
with
wordsArrayTemp = [NSMutableArray array];
meaningsArrayTemp = [NSMutableArray array];
Try to add one array simutaneously :
[[allWords array] addObject:wordsArray.array];
Hope it'll help
Try this:-
[allWords insertObject:[wordsArrayTemp copy] atIndex:i];
It should work.
I have an nsmutablearray(xmlParseArray) having values firstname and id, I want to copy only firstname into another nsmutablearray(copyArray).
How can I do this?
Assumption: your xmlParseArray contains number of objects all of which have a firstname property and and an id property
NSMutableArray* nameArray = [[xmlParseArray valueForKey: #"firstname"] mutableCopy];
// nameArray is an array you own.
-valueForKey: when sent to an array causes the message -valueForKey: to be sent to each of its elements and a new array to be constructed from the return values. The -mutableCopy ensures that the result is then turned into a mutable array as per your question.
I'm guessing you mean that the first array, xmlParseArray, contains a list of NSDictionary objects which each have objects attached to the keys "firstname" and "id". One way to accomplish that would be like this:
NSMutableArray *copyArray = [[NSMutableArray alloc] initWithCapacity:[xmlParseArray count]];
for(NSDictionary *dict in xmlParseArray)
if([dict objectForKey:#"firstname"])
[copyArray addObject:[dict objectForKey:#"firstname"]];
// ...do whatever with copyArray...
[copyArray release];
NSMutableArray *arr = [NSMutableArray arrayWithObject:[copyArray objectAtIndex:0]];
or
[arr addObject:[copyArray objectAtIndex:0]];
[arr addObject:[copyArray objectAtIndex:1]];
NSMutableArray *newArray = [oldArray mutableCopy];
or
NSMutableArray *newArray = [NSMutableArray arrayWithArray:oldArray];
be aware that the objects in the array aren't copied, just the array itself (references to objects are maintained).
I want to know that how can I add NSMutableArray in to an NSMutableArray so that previous data should not lost, and new data will be added on next indexes.
If you don't understand it then you can ask again to me,
I will appraise the right answer.
my code is as below
-(void)setArray1:(NSMutableArray *)arrayValueFromNew
{
self.myArray=arrayValueFromNew;
myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew];
for(int i=0;i<[myArray2 count];i++)
{
[myArray addObject:[myArray2 objectAtIndex:i]];
}
}
In your code, myArray and myArray2, both have same objects as you are assigning the arrayValueFromNew array to both. So it kind of doesn't make sense.
But to answer your question 'how to add one array to another?' do :
[mutableArray1
addObjectsFromArray:array2];
EDIT:
this is how your method should look
-(void)setArray1:(NSMutableArray *)arrayValueFromNew
{
if(!self.myArray)
{
self.myArray = arrayValueFromNew;
}
else
{
[self.myArray addObjectsFromArray:arrayValueFromNew];
}
}
Your 'myArray must be initialized. You can initialize it in viewDidLoad or init:
self.myArray = [[NSMutableArray alloc]
initWithCapacity:1];
NSMutableArray *array1 = [NSMutableArray array], *array2 = [NSMutableArray array];
// add some objects to the arrays
[array1 addObjectsFromArray:array2];
//array1 now contains all the objects originally in array1 and array2
This will work,
NSMutableArray *mutarr=[[NSMutableArray alloc]initWithArray: array1]
It looks like you just want a new copy of the old array. There is a handy function for that
NSMutableArray *newArray = [oldArray mutableCopy];
Remember that you've used copy in getting this array so you are responsible for managing the memory of newArray
EDIT
What is your code doing?
-(void)setArray1:(NSMutableArray *)arrayValueFromNew //1
{
self.myArray=arrayValueFromNew; //2
myArray2 = [[NSMutableArray alloc] initWithArray:arrayValueFromNew]; //3
for(int i=0;i<[myArray2 count];i++)
{
[myArray addObject:[myArray2 objectAtIndex:i]]; //4
}
}
This looks like a setter for a property array1
You are setting the property 'array' to arrayValueFromNew. Since I don't know whether this property has been declared with retain or copy I don't know whether array is a pointer to arrayValueFromNew or a pointer to a copy of arrayValueFromNew
You set myArray2 to be a new array that contains the objects of arrayValueFromNew
For each object in myArray2 (which are the objects from arrayValueFromNew. see point 3) you add this object to myArray. Assuming myArray is an NSMutableArray it started with the objects from arrayValueFromNew which you have now added again. It contains each item in arrayValueFromNew twice.