gpt4 book ai didi

objective-c - 无法创建唯一的 NSNumber 数组 - iOS

转载 作者:行者123 更新时间:2023-11-28 19:13:36 25 4
gpt4 key购买 nike

我在一些关于制作唯一数组的 SO 线程上读到,不幸的是,它对我不起作用。

背景信息-

  • 我有一个包含 20 个对象的 objectArray。
  • 一个带有字段 NSNumber * number 的 NSObject sampleData;
  • maxCount 是一个整数,用户可以输入他们想要的随机数。
  • 下面是我在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

如何使数组获得所有唯一编号?

最佳答案

你真的需要数组吗?为什么不使用 NSMutableSet?

From the docs :

The NSMutableSet class declares the programmatic interface to a mutable, unordered collection of distinct objects.

关于objective-c - 无法创建唯一的 NSNumber 数组 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com