我是Objective C编程的初学者,过去几天我一直在努力解决这个问题。 我有一个名为tasks的NSMutableArray,我在switch语句中访问它来改变这样的开关案例
- (IBAction)random:(id)sender {
int text = [[self.tasks objectAtIndex:x] intValue];
x++;
完美无瑕。但是当我尝试通过其中一种方法随机化/改组任务数组时,它只显示case 0
的{{1}}。我希望能够使用新洗牌的数组来更改开关案例。我是在正确的轨道上还是这样的话甚至可能?
ViewController.m
switch
ViewController.h
//
// ViewController.m
#import "ViewController.h"
#import "AudioToolbox/AudioToolbox.h"
#import <GameKit/GameKit.h>
#import "NSMutableArray_Shuffling.h"
@import GameKit;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navback.png"] forBarMetrics:UIBarMetricsDefault];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSMutableArray*) tasks {
tasks = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInteger:0] ,[NSNumber numberWithInteger:1] ,[NSNumber numberWithInteger:2] ,[NSNumber numberWithInteger:3] ,[NSNumber numberWithInteger:4] ,[NSNumber numberWithInteger:5] ,[NSNumber numberWithInteger:6] ,[NSNumber numberWithInteger:7] ,[NSNumber numberWithInteger:8] ,[NSNumber numberWithInteger:9] ,[NSNumber numberWithInteger:10] ,[NSNumber numberWithInteger:11] ,[NSNumber numberWithInteger:12] ,[NSNumber numberWithInteger:13] ,[NSNumber numberWithInteger:14] ,[NSNumber numberWithInteger:15] ,[NSNumber numberWithInteger:16] ,[NSNumber numberWithInteger:17] ,[NSNumber numberWithInteger:18] ,[NSNumber numberWithInteger:19] ,[NSNumber numberWithInteger:20] ,[NSNumber numberWithInteger:21] ,[NSNumber numberWithInteger:22], nil];
return tasks;
}
int x=0;
int z=9999;
int k=0;
//Neither of these shuffle methods work.
/*
- (void)shuffle
{
NSUInteger count = [tasks count];
if (count < 1) return;
for (NSUInteger i = 0; i < count - 1; ++i) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount);
[tasks exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
}
-(void)shuffle
{
shuffledTasks = [[GKRandomSource sharedRandom] arrayByShufflingObjectsInArray:tasks];
}
*/
// This is for checking if x is > than the number of elements in my tasks array, then reseting it and shuffling the array so a new order of elements is presented for the switch. But it works only for reseting the x.
-(void)reset{
if (x>22) {
x=0;
[self shuffle];
}
}
//The switch, which currently only pulls objectAtIndex:x from the original tasks array. No matter how I try to shuffle it.
- (IBAction)random:(id)sender {
int text = [[self.tasks objectAtIndex:x] intValue];
x++;
switch (text) {
case 0:
z=0;
//something magical will happen
[self reset];
break;
case 1:
z=1;
//something magical will happen
[self reset];
break;
case 2:
z=2;
//something magical will happen
[self reset];
break;
case 3:
z=3;
//something magical will happen
[self reset];
break;
case 4:
z=4;
//something magical will happen
[self reset];
break;
//cases 5-21…
case 22:
z=22;
//something magical will happen
[self reset];
break;
default:
z=9999;
}
}
- (void)dealloc {
[super dealloc];
}
@end
答案 0 :(得分:0)
你在哪里设置x的值? 无论哪种方式添加此行
int x = arc4random_uniform(self.tasks - 1);
高于这个
int text = [[self.tasks objectAtIndex:x] intValue];
并且它应该完美地工作(假设self.tasks中的元素是&#34;值类型&#34;否则你将在这里得到零:
int text = [[self.tasks objectAtIndex:x] intValue];