Iphone 读取数组
我使用以下代码创建随机数并将其存储在数组中Iphone 读取数组,iphone,objective-c,nsarray,Iphone,Objective C,Nsarray,我使用以下代码创建随机数并将其存储在数组中 NSMutableSet *aSet = [NSMutableSet setWithCapacity:6]; while([aSet count]<=6){ int Randnum = arc4random() % 12; [aSet addObject:[NSNumber numberWithInt:Randnum]]; } NSArray *arrayOfUniqueRandomNumbers = [aSet all
NSMutableSet *aSet = [NSMutableSet setWithCapacity:6];
while([aSet count]<=6){
int Randnum = arc4random() % 12;
[aSet addObject:[NSNumber numberWithInt:Randnum]];
}
NSArray *arrayOfUniqueRandomNumbers = [aSet allObjects];
NSMutableSet*aSet=[NSMutableSet setWithCapacity:6];
而([aSet count]您可以执行以下操作:
for (NSNumber *val in arrayOfUniqueRandomNumbers) {
int i = [val intValue];
...
}
显然,您只有6个随机数;您不需要使用NSMutableSet,而是将其读入NSArray—直接使用NSArray来存储6个值。
for (NSNumber *val in arrayOfUniqueRandomNumbers) {
int i = [val intValue];
...
}