Ios 在NSLog中显示数组
我已经做了一个基本的洗牌算法 我想通过nslog检查它是否正常工作 它会使应用程序崩溃,并在nslog中设置断点 有人能帮忙吗Ios 在NSLog中显示数组,ios,objective-c,Ios,Objective C,我已经做了一个基本的洗牌算法 我想通过nslog检查它是否正常工作 它会使应用程序崩溃,并在nslog中设置断点 有人能帮忙吗 -(IBAction)suffle:(id)sender { NSMutableArray *arrayExample = [[NSMutableArray alloc] init]; [arrayExample addObject:[NSNumber numberWithInt:1]]; [arrayExample addObject:[NS
-(IBAction)suffle:(id)sender
{
NSMutableArray *arrayExample = [[NSMutableArray alloc] init];
[arrayExample addObject:[NSNumber numberWithInt:1]];
[arrayExample addObject:[NSNumber numberWithInt:2]];
[arrayExample addObject:[NSNumber numberWithInt:3]];
[arrayExample addObject:[NSNumber numberWithInt:4]];
for (int i = 0; i < [arrayExample count]; ++i) {
int r = (random() % [arrayExample count]);
[arrayExample exchangeObjectAtIndex:i withObjectAtIndex:r];
NSLog(@"%@", [arrayExample description]);
}
}
-(iAction)suffle:(id)发送者
{
NSMutableArray*arrayExample=[[NSMutableArray alloc]init];
[arrayExample addObject:[NSNumber numberwhithint:1]];
[arrayExample addObject:[NSNumber numberwhithint:2]];
[arrayExample addObject:[NSNumber numberwhithint:3]];
[arrayExample addObject:[NSNumber numberwhithint:4]];
对于(int i=0;i<[arrayExample count];++i){
int r=(随机()%[arrayExample count]);
[阵列示例交换对象索引:i与对象索引:r];
NSLog(@“%@,[arrayExample description]);
}
}
这是我的代码也许这个程序对你有帮助
- (NSMutableArray *)generateRandomNumbers:(NSNumber *)numberOfElements {
NSMutableArray *arrayRandomPositions = [[NSMutableArray alloc] init];
// First create sequential numbers array
for (int i=0; i<[numberOfElements integerValue]; i = i +1) {
[arrayRandomPositions addObject:[NSNumber numberWithInt:i]];
}
// Make the shuffle
for (int i = 0; i < [numberOfElements integerValue]; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = (int)[numberOfElements integerValue] - i;
int n = arc4random_uniform(nElements) + i;
[arrayRandomPositions exchangeObjectAtIndex:i withObjectAtIndex:n];
}
return arrayRandomPositions;
}
-(NSMutableArray*)生成器域编号:(NSNumber*)numberOfElements{
NSMutableArray*arrayRandomPositions=[[NSMutableArray alloc]init];
//首先创建序列号数组
对于(int i=0;方法名称可能有问题,因为它会出现错误您必须调用以下内容:NSMutableArray*PositionCardsName=[self-getRandomNumbers:[NSNumber numberWithInt:8]];我在一些应用程序中实际使用了此例程。它需要一个;在(NSMutableArray*之后)我如何将此应用于按钮并在NSLog中打印?您发布的代码有什么问题?您的问题不清楚。好的,那么您尝试从阵列中记录单个元素的尝试在哪里?同样,您遇到了什么问题?如果您的应用程序崩溃(您没有提到)然后,您需要使用有关崩溃的详细信息更新您的问题,包括完整的错误消息。请参阅不要使用random()
,它不是随机的。而是使用arc4random()
或arc4random\u uniform()
。在这种情况下:arc4random\u uniform([arrayExample count])
“这并不能解决问题,但对于随机操作是必需的。