虽然(不)循环冻结应用程序
问题描述:
我的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];
}
答
我的猜测是因为你的while循环内重新申报PlistRandom
,内声明的变量可以是超出范围的while
conditionis评估点。您的问题,我认为这是一个范围的问题,只是改变环路这一点,看看是否能工程:
while (![PlistRandom isEqual: RAMArray])
{
PlistRandom = [PlistArray objectAtIndex: random()%[PlistArray count]];
}
这意味着循环是无限的 – 2013-05-08 15:43:30
又如何解决呢? – Berendschot 2013-05-08 15:44:28
通过找出是什么使其无限 – 2013-05-08 15:46:43