我创建了一个对象数组。这些对象有一个公共变量correct
。当我关闭 xcode 优化时,我的项目中的所有内容都运行良好,但是当我设置任何类型的优化时,事情就会出错。我第一次创建这些对象中的一个correct
时,从另一个类访问时,变量总是返回 false。但随后创建此类对象会返回正确的值correct
。这是我的代码和问题的片段。
。H
@interface ShellQuestionAnswer : UIView
{
...
@public
bool correct;
...
}
.m
-(void)setLetter:(NSString*)letter forAnswer:(XMLNode*)answer withResponse:(XMLNode*)response
{
...
correct = [[[answer attributes] valueForKey:@"Correct"] isEqualToString:@"true"];
//Putting NSLog here shows "correct" to be true
}
以及使用这个对象的类:
-(void)addQuestion:(XMLNode*) rootNode ID:(NSString*)ID
{
...
answer1 = [[ShellQuestionAnswer alloc] initWithFrame:CGRectMake(0, currentY, textContainer.frame.size.width, 50)];
[answer1 setLetter:@"A" forAnswer:[answerstextArray objectAtIndex:0] withResponse:[responsestextArray objectAtIndex:0]];
currentY += answer1.frame.size.height + QUESTION_SEPARATION;
if(!answerObjects)
{
answerObjects = [[NSMutableArray alloc] init];
}
else
{
[answerObjects removeAllObjects];
}
[answerObjects addObject:answer1];
answer2 = [[ShellQuestionAnswer alloc] initWithFrame:CGRectMake(0, currentY, textContainer.frame.size.width, 50)];
[answer2 setLetter:@"B" forAnswer:[answerstextArray objectAtIndex:1] withResponse:[responsestextArray objectAtIndex:1]];
currentY += answer2.frame.size.height + QUESTION_SEPARATION;
[answerObjects addObject:answer2];
[textContainer addSubview:answer1];
[answer1 setUserInteractionEnabled:YES];
[textContainer addSubview:answer2];
[answer2 setUserInteractionEnabled:YES];
if([answerstextArray count] > 2)
{
answer3 = [[ShellQuestionAnswer alloc] initWithFrame:CGRectMake(0, currentY, textContainer.frame.size.width,50)];
[answer3 setLetter:@"C" forAnswer:[answerstextArray objectAtIndex:2] withResponse:[responsestextArray objectAtIndex:2]];
[textContainer addSubview:answer3];
currentY += answer3.frame.size.height + QUESTION_SEPARATION;
[answerObjects addObject:answer3];
[answer3 setUserInteractionEnabled:YES];
}
if([answerstextArray count] > 3)
{
answer4 = [[ShellQuestionAnswer alloc] initWithFrame:CGRectMake(0, currentY, textContainer.frame.size.width, 50)];
[answer4 setLetter:@"D" forAnswer:[answerstextArray objectAtIndex:3] withResponse:[responsestextArray objectAtIndex:3]];
[textContainer addSubview:answer4];
currentY += answer4.frame.size.height + QUESTION_SEPARATION;
[answerObjects addObject:answer4];
[answer4 setUserInteractionEnabled:YES];
}
[textContainer setNeedsDisplay];
for(ShellQuestionAnswer *answer in answerObjects)
{
if(answer->correct) //<-- always returns false the first time I run this function
{
if(!self.correctAnswer)
{
self.correctAnswer = [[NSMutableArray alloc] init];
}
[self.correctAnswer addObject:answer];
}
}
}
我已经记录了所有内容,但不明白为什么在answer->correct
对象外部返回 false,但在answer
对象内部,变量correct
实际上是 true。这仅在优化开启(任何模式)并且仅在使用此类的第一个实例时发生。
如果我将correct
变量更改为属性而不是公共类成员变量,则问题不再存在。我对这个解决方案很好,但我不明白为什么会发生这种情况。这个问题可能出现在我的代码中的任何其他类中,直到客户在第一次运行时抱怨随机怪癖,我才知道在哪里。这是我无法接受的。有人对为什么优化会弄乱我的代码有任何想法吗?
编辑:为了更清楚,这是我正在使用的测试用例。一种有效,一种无效(以上述方式)。
在职的:
@interface ShellQuestionAnswer : UIView
{
...
@public
//bool correct;
...
}
@property (nonatomic, assign) bool correct;
不工作:
@interface ShellQuestionAnswer : UIView
{
...
@public
bool correct;
...
}
这些是为获得工作集而更改的唯一行(除了answer->correct
更改answer.correct
为属性案例的 for 循环内部)。