I read on some SO thread on making an unique array, unfortunately, it did not work for me.
BackGround Info –
- I have an objectArray filled with 20 object in it.
- a NSObject sampleData with a field NSNumber * number;
- maxCount is a int which user input how many random numbers they want.
- below is my code in Xcode and the return for calling the function
.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);
}
return value from 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
How do I make it such that the array will get all unique number?
The problem lies here
You are checking for
NSNumberinstances but you don’t putNSNumberinside the array, you are putting something completely different (currentData).If you need both ordering (do you really need ordering?) and uniqueness, a combination of
NSArrayandNSSetcan be used for effectiveness. Of course, this is more effective only if you do lots of operations which needs the uniqueness check. If you only want to create an array and and use it without changes, your implementation (using[NSArray containsObject:]will be better.Abstracting everything into a special class is always a good solution:
@interface UniqueArray () @property (nonatomic, strong, readwrite) NSArray* elements; @property (nonatomic, strong, readwrite) NSMutableSet* set; @end @implementation UniqueArray - (id)init { self = [super init]; if (!self) { return nil; } self.elements = [NSMutableArray array]; self.set = [NSMutableSet set]; return self; } - (BOOL)addObject:(id)object { if ([self.set containsObject:object]) { return NO; } [self.set addObject:object]; [(NSMutableArray*) self.elements addObject:object]; return YES; } @end