这就是我想做的。每次我的viewDidLoad启动时,都要得到7个随机的、不重复的数字.我得到了它来创作随机,但我一直试图清除NSMutableSet时,它加载得到一个新的集,我有麻烦。NSLog清楚地显示了NSMutableSet中没有任何东西,但是它总是按照相同的顺序得到相同的数字?
// Create set
NSMutableSet *mySet = [NSMutableSet setWithCapacity:6];
// Clear set
NSMutableSet *mutableSet = [NSMutableSet setWithSet:mySet];
[mutableSet removeAllObjects];
mySet = mutableSet;
NSLog(@"mutableSet: %@", mutableSet); // Shows nothing
NSLog(@"mySet: %@", mySet); // Shows nothing
// Assign random numbers to the set
while([mySet count]<=6){
int Randnum = arc4random() % 7+1;
[mySet addObject:[NSNumber numberWithInt:Randnum]];
}
NSLog(@"mySet1: %@", mySet); // Always shows 5,1,6,2,7,3,4 ???发布于 2014-09-12 20:31:12
NS(Mutable)Set是一个无序的集合,它不会在插入元素时保留元素的顺序。因此,您的输出仅显示集合包含从1到7的数字。
您有不同的选项来获得预期的输出。
NSMutableOrderedSet代替。https://stackoverflow.com/questions/25814921
复制相似问题