0

while ループが機能していないようです。このビューをロードすると、アプリがフリーズします。while ループを含むコードの一部を削除すると、アプリはフリーズしません。

私が探しているのは、同じ配列が 2 回選択されないようにするコードです。

@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];
}
4

1 に答える 1

3

PlistRandom私の推測では、while ループ内で再宣言しているため、while条件が評価された時点で、内側で宣言された変数が範囲外になる可能性があります。あなたの問題はスコーピングの問題だと思います。ループをこれに変更して、それが機能するかどうかを確認してください:

while (![PlistRandom isEqual: RAMArray])
{
    PlistRandom = [PlistArray objectAtIndex: random()%[PlistArray count]];
}
于 2013-05-08T15:58:56.153 に答える