2016-09-15 5 views
1

私はObjective Cプログラミングの初心者です。私はこれをここ数日間苦労しています。 は、私は完璧に動作このNSArrayをシャッフルし、シャッフルされた配列を使用してスイッチのケースを変更します。 Objective C

- (IBAction)random:(id)sender { int text = [[self.tasks objectAtIndex:x] intValue]; x++;

のようなスイッチの例を変更するには、switch文でアクセスしてることを、NSMutableArrayのタスクと呼ばれています。しかし、タスク配列をランダム化/シャッフルしようとすると、以下のいずれかの方法でcase 0switchが表示されます。スイッチのケースを変更するために新しくシャッフルされた配列を使用できるようにしたい。正しい軌道に乗っているのですか、これも可能ですか?あなたはxの値を設定してください

ViewController.m

// 
// 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 

ViewController.h

// 
// ViewController.h 


#import <UIKit/UIKit.h> 
#import "Accounts/Accounts.h" 
@import GameKit; 


@interface ViewController : UIViewController { 

    //Here should be declared everything for the Main.storyboard but it's unnecessarily long and it works, so no need to post it here. 

    NSMutableArray *tasks; 
    NSArray *shuffledTasks; 
    NSMutableArray *zamichano; 

} 




- (void)TimerCount; 
- (void)shuffle; 

- (IBAction)random:(id)sender; 
//Again, here would be things for the storyboard. 



@end 

答えて

0

? いずれかの方法では、この1

int text = [[self.tasks objectAtIndex:x] intValue]; 

上記

int x = arc4random_uniform(self.tasks - 1); 

次の行を追加し、それがself.tasksの要素は、「値型」であることを仮定して(完全に動作するはずそうでなければ、ゼロを得るでしょうここ:

int text = [[self.tasks objectAtIndex:x] intValue]; 
関連する問題