我有一个名为 putNumberUsed 的 NSMutableArray。它包含以下对象@"blah1、@"blah2"、@"blah3"、@"blah4"。我想随机打乱这些对象,例如,如果我选择:
[putNumberUsed objectAtIndex:0]
除了“blah1”,它会给我任何东西。我该怎么做呢?以下是我到目前为止使用的代码:
NSMutableArray *putNumbersUsed = [[NSMutableArray alloc] arrayWithObjects:@"blah1",@"blah2",@"blah3",@"blah4",nil];
最佳答案
我认为,您可以为此编写一个循环。请检查以下代码,
for (int i = 0; i < putNumberUsed.count; i++) {
int randomInt1 = arc4random() % [putNumberUsed count];
int randomInt2 = arc4random() % [putNumberUsed count];
[putNumberUsed exchangeObjectAtIndex:randomInt1 withObjectAtIndex:randomInt2];
}
我认为这可能对您有用。
关于objective-c - 强制洗牌 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224014/