2014-12-25 34 views
0

我試圖用SWITCH和GCD加載我的數組,以便不停止主線程。但Xcode不能構建我的代碼。請任何人都可以解釋我爲什麼可以用這種方式使用GCD?帶gcd的開關盒

switch (number) { 
     case 0: 
      //case 0:exercise_name="Стойка"; 
      break; 

     case 1: 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       @synchronized (self.handsMassage){ 
        /* 
        NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]                             pathForResource:@"sound" ofType:@"wav"]]; 
        _click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil]; 
        self.click.delegate = self; 
        */ 
        for (int i = 1; i <= 22; i++) { 
         [self.arrayOfExercise addObject:(id)[UIImage imageNamed:[NSString stringWithFormat:@"ml_%i", i]].CGImage]; 
        } 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.playButton setHidden:NO]; 
       }); 
      }); 
      break; 

     case 2: 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       @synchronized (self.loinMassage){ 
        /* 
        NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]                             pathForResource:@"sound" ofType:@"wav"]]; 
        _click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil]; 
        self.click.delegate = self; 
        */ 
        for (int i = 1; i <= 9; i++) { 
         [self.arrayOfExercise addObject:(id)[UIImage imageNamed:[NSString stringWithFormat:@"mp_%i", i]].CGImage]; 
        } 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.playButton setHidden:NO]; 
       }); 
      }); 
      break; 

XCode打印我開關保護模式下的情況?

+1

什麼是編譯器錯誤信息? –

+0

@KazukiSakamoto [img](http://s013.radikal.ru/i324/1412/8f/3d17570140d0.png) –

回答

5

在每種情況下放置{}。像這樣:

switch (number) { 
     case 0: 
     { 
      //case 0:exercise_name="Стойка"; 
      break; 
     } 
     case 1: 
     { 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       @synchronized (self.handsMassage){ 
        /* 
        NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]                             pathForResource:@"sound" ofType:@"wav"]]; 
        _click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil]; 
        self.click.delegate = self; 
        */ 
        for (int i = 1; i <= 22; i++) { 
         [self.arrayOfExercise addObject:(id)[UIImage imageNamed:[NSString stringWithFormat:@"ml_%i", i]].CGImage]; 
        } 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.playButton setHidden:NO]; 
       }); 
      }); 
      break; 
     } 
     case 2: 
     { 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       @synchronized (self.loinMassage){ 
        /* 
        NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]                             pathForResource:@"sound" ofType:@"wav"]]; 
        _click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil]; 
        self.click.delegate = self; 
        */ 
        for (int i = 1; i <= 9; i++) { 
         [self.arrayOfExercise addObject:(id)[UIImage imageNamed:[NSString stringWithFormat:@"mp_%i", i]].CGImage]; 
        } 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.playButton setHidden:NO]; 
       }); 
      }); 
      break; 
     } 

這應該解決它。

+0

是)謝謝)但爲什麼是這樣呢? –

+0

因爲像if語句一樣,你只能在一個case後面放一個語句。你應該在防禦性條件之後總是放置括號。您將避免錯誤並清除代碼。隨着代碼的長度,你應該將其分解爲另一種方法。 – uchuugaka

+3

uchuugaka的評論是不正確的。在案例:標籤之後,您並不侷限於單個陳述。案例標籤指示執行的開始位置,並繼續執行直到switch語句結束(或直到遇到中斷)。您不能在案例標籤之後放置聲明。 –