Objective c setTitle中的NSMutableArray

Objective c setTitle中的NSMutableArray,objective-c,Objective C,我在NSMutableArray中存储了4个从1到4的唯一数字。我通过使用以下代码实现了这一点: storeArray = [[NSMutableArray alloc] init]; BOOL record = NO; int x; for (int i=1; [storeArray count] < 4; i++) //Loop for generate different random values { x = arc4ran

我在NSMutableArray中存储了4个从1到4的唯一数字。我通过使用以下代码实现了这一点:

    storeArray = [[NSMutableArray alloc] init];
    BOOL record = NO;
    int x;

    for (int i=1; [storeArray count] < 4; i++) //Loop for generate different random values
    {
        x = arc4random() % 4;//generating random number
        if(i==1)//for first time
       {
            [storeArray addObject:[NSNumber numberWithInt:x]];
        }
        else
        {
            for (int j=0; j<= [storeArray count]-1; j++)
            {
               if (x ==[[storeArray objectAtIndex:j] intValue])
                    record = YES;
            }

            if (record == YES)
            {
                record = NO;
            }
            else
            {
                [storeArray addObject:[NSNumber numberWithInt:x]];
            }
        }
    }

我该怎么做?因为当我这样做时,我得到了线程sigbrt错误?

问题是,您的算法有问题,当由于试图保持数字唯一而发生冲突时,您不会记录任何内容,因此数组可能不总是预期长度。事实上,在你的情况下,这种情况发生的几率为91%,所以看起来这种情况一直都在发生

与其尝试编写自己的算法,不如使用现有的类。只需使用NSSet即可保证数组中数字的唯一性

NSMutableSet *set = [[NSMutableSet alloc] init];
while(set.count < 4) {
  int x = arc4random() % 4;
  [set addObject:[NSNumber numberWithInteger:x]];
}

NSArray * storeArray = [set allObjects];

数组的索引从零开始,而不是从一开始。如果一个数组中有四个元素,则可以使用storeArray[0]通过storeArray[3]访问它们。如何声明questions变量?如果您发布了SIGABRT的堆栈跟踪,这将非常有用。
NSMutableSet *set = [[NSMutableSet alloc] init];
while(set.count < 4) {
  int x = arc4random() % 4;
  [set addObject:[NSNumber numberWithInteger:x]];
}

NSArray * storeArray = [set allObjects];