用gcd切换机箱

时间:2014-12-25 07:25:50

标签: objective-c xcode concurrency grand-central-dispatch

我试图使用SWITCH和GCD加载我的数组,以便不停止主线程。但Xcode无法构建我的代码。请任何人解释一下为什么我可以用这种方式使用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 :(得分:6)

在每个案例周围加上{}。像这样:

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;
        }

那应该解决它。