虽然(不)循环冻结应用程序

时间:2013-05-08 15:40:23

标签: objective-c while-loop

我的while循环似乎不起作用。加载此视图时,应用程序会冻结。 当我删除包含while循环的代码部分时,应用程序将不会冻结。

我正在搜索的是一段代码,它会导致相同的数组不会被选中两次。

@interface ThirdViewController ()

@end

@implementation ThirdViewController

...
NSString * Answer = @"";
NSArray * RAMArray;

...

- (void)NewQuestion
{
    NSString * PlistString = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
    NSMutableArray * PlistArray = [[NSMutableArray alloc]initWithContentsOfFile:PlistString];
    NSArray *PlistRandom = [PlistArray objectAtIndex: random()%[PlistArray count]];

    while (![PlistRandom isEqual: RAMArray])
    {
        NSArray *PlistRandom = [PlistArray objectAtIndex: random()%[PlistArray count]];
    }

    RAMArray = PlistRandom;
    ...
}

- (void)Check:(NSString*)Choise
{
    ...

    if ([Choise isEqualToString: Answer])
    {
        ...
        [self NewQuestion];
    }
}

- (IBAction)AnsButA:(id)sender
{
    UIButton *ResultButton = (UIButton *)sender;
    NSString *Click = ResultButton.currentTitle;

    [self Check:Click];
}

1 个答案:

答案 0 :(得分:3)

我的猜测是,因为你在while循环中重新声明PlistRandom,所以内部声明的变量可能在while条件被评估的范围之外超出范围。我认为你的问题是一个范围问题,只需将循环改为此,看看是否有效:

while (![PlistRandom isEqual: RAMArray])
{
    PlistRandom = [PlistArray objectAtIndex: random()%[PlistArray count]];
}