我在一些关于制作独特数组的 SO 线程上阅读,不幸的是,它对我不起作用。
背景资料 -
- 我有一个 objectArray,里面有 20 个对象。
- 带有字段 NSNumber * number 的 NSObject sampleData;
- maxCount 是一个 int,用户可以输入他们想要的随机数。
- 下面是我在 Xcode 中的代码和调用函数的返回
.m
-(IBAction) testButton
{
[self chooseNumber];
}
-(void)chooseNumber
{
maxCount = [numberOfClues.text intValue];
// NSInteger rdmNumber = arc4random()%objectArray.count;
int count = 0;
do {
NSInteger rdmNumber = arc4random()%objectArray.count;
if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]])
{
currentData.number = [NSNumber numberWithInt:rdmNumber];
[dataArray addObject:currentData];
currentData=nil;
currentData= [[sampleData alloc]init];
count++;
NSLog(@"random no - %d",rdmNumber);
}
} while (count < maxCount);
NSLog(@"Array got %d numbers",dataArray.count);
}
从 NSLog 返回值
2012-11-29 08:26:50.888 randomNumbers[1255:11303] random no - 5
2012-11-29 08:26:50.892 randomNumbers[1255:11303] random no - 8
2012-11-29 08:26:50.926 randomNumbers[1255:11303] random no - 5
2012-11-29 08:26:50.930 randomNumbers[1255:11303] random no - 10
2012-11-29 08:26:50.933 randomNumbers[1255:11303] random no - 4
2012-11-29 08:26:50.946 randomNumbers[1255:11303] random no - 10
2012-11-29 08:26:50.949 randomNumbers[1255:11303] random no - 9
2012-11-29 08:26:50.952 randomNumbers[1255:11303] random no - 12
2012-11-29 08:26:50.955 randomNumbers[1255:11303] random no - 0
2012-11-29 08:26:50.957 randomNumbers[1255:11303] random no - 15
2012-11-29 08:26:50.960 randomNumbers[1255:11303] random no - 1
2012-11-29 08:26:50.963 randomNumbers[1255:11303] random no - 8
2012-11-29 08:26:50.964 randomNumbers[1255:11303] random no - 18
2012-11-29 08:26:50.966 randomNumbers[1255:11303] random no - 14
2012-11-29 08:26:50.968 randomNumbers[1255:11303] random no - 12
2012-11-29 08:26:50.977 randomNumbers[1255:11303] random no - 14
2012-11-29 08:26:50.980 randomNumbers[1255:11303] random no - 3
2012-11-29 08:26:50.983 randomNumbers[1255:11303] random no - 6
2012-11-29 08:26:50.986 randomNumbers[1255:11303] random no - 15
2012-11-29 08:26:50.989 randomNumbers[1255:11303] random no - 7
2012-11-29 08:26:50.992 randomNumbers[1255:11303] Array got 20 numbers
如何使数组获得所有唯一编号?