0

If i have 2 NSMutableArray declared in .h, synthesize in .m files, how can i copy whole contents of array 1 to 2? actually the same array.

I tried using this code only:

[self.data addObjectsFromArray:self.temparray]

to copy data from temparray to data.

doesn't seems to work.....

2
  • hi all, thks for the reply, it got me thinking :) manage to solve! thks again
    – Stefan
    Aug 23, 2010 at 7:27
  • Thanks for sharing your solution with us.... oh wait ... you didn't Dec 8, 2010 at 12:31

2 Answers 2

0

Did you initialize your self.data at all? I.e. self.data = [NSMutableArray array]; somewhere.

If you did, please post some code.

3
  • self.data = [NSArray arrayWithContentsOfFile:dataPath]; its load from plist, which could be empty at first. i save some table cell contents to another temp array. and than i would like to save it to self.data.
    – Stefan
    Aug 22, 2010 at 20:43
  • or here is what i am trying to do: selected a table cell, save it to array and before controller exit save array to file. now implement an alertview, if its ok is press than the table cell will be save to array. i can only thinking of using a temp array to save the table cell 1st, and when ok is press, transfer the temp array to original array. or is there other better ways?
    – Stefan
    Aug 22, 2010 at 21:03
  • If you edit your question to include the code that would help greatly.
    – Eiko
    Aug 22, 2010 at 21:46
0
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray

is the method your looking for.

5
  • This gives a new array instead of reusing the existing one. Why should he want to use this over the other?
    – Eiko
    Aug 22, 2010 at 20:52
  • @Stefan, why don't you tell us what "doesn't seem to work" means. Aug 22, 2010 at 21:19
  • hi there, self.temparray is not copying to self.data array. i type the following code only: [self.data arrayByAddingObjectsFromArray:self.tempArray]; no errors, but its not copying. thks
    – Stefan
    Aug 22, 2010 at 21:25
  • whats the output of [self.tempArray count] ? Aug 22, 2010 at 21:30
  • @Stefan, what are you doing with the array that is returned by -[NSArray arrayByAddingObjectsFromArray]. Are you sure you're not just throwing the new array away? Also, when you say "copy" are you trying to add all the elements of tempArray to self.data? Or are you trying to replace all the elements of self.data with the objects from tempArray? Aug 22, 2010 at 21:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.