views:

773

answers:

2

i have two different classes with named indexViewController and flashCardQuestionViewController.

in the index view controller i have table filled with an array.

now i am getting some data from the database..

-(void)getMultipleChoiceAnswer

{

if(optionid!=nil)
 [optionid removeAllObjects];
else
 optionid = [[NSMutableArray alloc] init];

if(optionText!=nil)
 [optionText removeAllObjects];
else
 optionText = [[NSMutableArray alloc] init];


clsDatabase *clsDatabaseObject = [[clsDatabase alloc] init];
sqlite3_stmt *dataRows = [clsDatabaseObject getDataset:"select optionID,OptionText from flashCardMultipleAnswer where questionId=1"];
while(sqlite3_step(dataRows) == SQLITE_ROW)
{  

 [optionid addObject:[NSNumber numberWithInt:sqlite3_column_int(dataRows,0)]];
 [optionText addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,1)]]; 

}
sqlite3_finalize(dataRows);
[clsDatabaseObject release];

}

and i am calling this method in the ViewDidLoad Method Of the indexviewcontroller.

now i have another NSMutable array in the flashCardQuestionViewController named listNoOfOptionsInQuestion..

i want to fill listNoOfOptionsInQuestion with the array of indexviewcontrollers array optionText

+4  A: 

There are a number of ways to copy arrays: you can either use -[NSArray copy] to get an immutable copy, or -[NSArray mutableCopy] for a mutable copy. Don't forget that copy adds a reference so you'll need a release or autorelease somewhere (if you're not using GC that is).

Alternatively, you can use -[NSMutableArray addObjectsFromArray:].

Given your example, it looks like you want to do something like this at the end:

[flashCardQuestionViewController setListNoOfOptionsInQuestion:optionText];

And then in FlashCardQuestionViewController, you want something like:

- (void)setListNoOfOptionsInQuestion:(NSArray *)options
{
  if (options != listNoOfOptionsInQuestion) {
    [listNoOfOptionsInQuestion release];
    listNoOfOptionsInQuestion = [options mutableCopy];
  }
}
Chris Suter
this does not work.i have both NSMUtableArray in Both Classes
Rahul Vyas
You’ll need to be a bit more specific about what does not work. Post some code maybe.
Chris Suter
i have put the code now tell me the solution
Rahul Vyas
+1  A: 

Rahul,

Do you really need to have a completely different copy of the MutableArray in each object. Would it be possible to have both objects point to the same array? For instance:

ClassOne  *one = [[ClassOne alloc] init];
ClassTwo  *two = [[ClassTwo alloc] init];

//   build mutable array mArray
//   ...

one.objectArray = mArray;
two.objectArray = mArray;

Or do you need to make changes to the two arrays in different ways? The try this (as suggested by Chris above) :

ClassOne  *one = [[ClassOne alloc] init];
ClassTwo  *two = [[ClassTwo alloc] init];

//   build mutable array mArray
//   ...

one.objectArray = mArray;
two.objectArray = [mArray mutableCopy];

again, if this isn't what you need then you'll have to give us a more precise question or problem that we can identify.

littleknown