Objective c 将对象添加到for循环中的NSMutableArray中,可以在所有位置复制该对象
我有一个包含问题的sqlite数据库,我想编写一个函数从中获取N个随机问题。我使用具有n次迭代的for循环。对于每次迭代,我都会得到一个随机ID的问题,这个ID不会重复。我复制“question”类对象变量中每列的元素。在这里之前,一切都是完美的。我已经使用NSLog打印了所有函数中的问题,在我尝试将最后一个对象添加到NSMutablearray之前,所有过程都是正确的。所有位置都被覆盖。 这是我的代码:Objective c 将对象添加到for循环中的NSMutableArray中,可以在所有位置复制该对象,objective-c,nsmutablearray,Objective C,Nsmutablearray,我有一个包含问题的sqlite数据库,我想编写一个函数从中获取N个随机问题。我使用具有n次迭代的for循环。对于每次迭代,我都会得到一个随机ID的问题,这个ID不会重复。我复制“question”类对象变量中每列的元素。在这里之前,一切都是完美的。我已经使用NSLog打印了所有函数中的问题,在我尝试将最后一个对象添加到NSMutablearray之前,所有过程都是正确的。所有位置都被覆盖。 这是我的代码: -(NSMutableArray*)getNQuestions:(int)n {
-(NSMutableArray*)getNQuestions:(int)n
{
question *aQuestion=[[question alloc]init];
NSMutableArray *arrayOfChoosenQuestions=[[NSMutableArray alloc]initWithCapacity:n];
NSMutableIndexSet *choosenQuestionsIDs=[[NSMutableIndexSet alloc]init];
FMResultSet *rs;
int questionID;
for (int i=0; i<n; i++) {
questionID = (arc4random()%(NUMBER_OF_AVAILABLE_QUESTIONS-1))+1;
while ([choosenQuestionsIDs containsIndex:questionID]) {
questionID = (arc4random()%(NUMBER_OF_AVAILABLE_QUESTIONS-1))+1;
}
[choosenQuestionsIDs addIndex:questionID];
rs = [database executeQueryWithFormat:@"SELECT * FROM questions WHERE Questions.ID=%d", questionID];
if ([rs next] ) {
aQuestion.statement=[rs stringForColumn:@"Question"];
aQuestion.rightAnswer=[rs stringForColumn:@"Right_Answer"];
aQuestion.wrongAnswer1=[rs stringForColumn:@"Wrong_Answer_1"];
aQuestion.wrongAnswer2=[rs stringForColumn:@"Wrong_Answer_2"];
aQuestion.image=[rs stringForColumn:@"image"];
}
[arrayOfChoosenQuestions addObject:aQuestion];
}
return arrayOfChoosenQuestions;
}
-(NSMutableArray*)getNQuestions:(int)n
{
问题*aQuestion=[[question alloc]init];
NSMutableArray*arrayOfChoosenQuestions=[[NSMutableArray alloc]initWithCapacity:n];
NSMutableIndexSet*选择问题SID=[[NSMutableIndexSet alloc]init];
FMResultSet*rs;
int问题ID;
对于(int i=0;i您反复使用相同的问题
对象(即aQuestion
),因此您实际上是在修改相同的基础对象并将其插入数组中
基本上,在您当前的代码中,当您从aQuestion
读/写时,您引用的是相同的内存块。所有修改都将转到内存中的相同位置。如您所见,更改“传播”对于其他对象,因为它们实际上是aQuestion
中指针引用的同一内存块
对于每个循环,您必须创建一个新的question
对象以容纳新数据。您反复使用相同的question
对象(即aQuestion
),因此您实际上是在修改相同的基础对象并将其插入数组中
基本上,在您当前的代码中,当您从aQuestion
读/写时,您引用的是相同的内存块。所有修改都将转到内存中的相同位置。如您所见,更改“传播”对于其他对象,因为它们实际上是aQuestion
中指针引用的同一内存块
对于每个循环,您必须创建一个新的问题
对象以容纳新数据。除了NHAHDH所说的,您的方法过于复杂。可能可以简化为:
-(NSMutableArray*)fetchNQuestions:(int)n {
NSMutableArray *questions = [NSMutableArray array];
FMResultSet *rs = [database executeQueryWithFormat:@"SELECT * FROM questions ORDER BY RANDOM LIMIT %d", n];
while ([rs next]) {
Question *q = [[Question alloc] init];
[q setStatement:[rs stringForColumn:@"Question"]];
...
[questions addObject:q];
// if you're not using ARC:
[q release];
}
return questions;
}
除了NHAHDH所说的,你的方法过于复杂。它可能被简化为:
-(NSMutableArray*)fetchNQuestions:(int)n {
NSMutableArray *questions = [NSMutableArray array];
FMResultSet *rs = [database executeQueryWithFormat:@"SELECT * FROM questions ORDER BY RANDOM LIMIT %d", n];
while ([rs next]) {
Question *q = [[Question alloc] init];
[q setStatement:[rs stringForColumn:@"Question"]];
...
[questions addObject:q];
// if you're not using ARC:
[q release];
}
return questions;
}
这不可能是正确的:[arrayOfChoosenQuestions addIndex:questionID];
。抱歉,我已将代码从我的语言翻译过来,犯了这个错误。这不可能是正确的:[arrayOfChoosenQuestions addIndex:questionID];
。抱歉,我已将代码从我的语言翻译过来,犯了这个错误