用gcd切换机箱

Сер*_*нич 0 concurrency xcode objective-c 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;
Run Code Online (Sandbox Code Playgroud)

XCode打印保护模式下的开关盒?

ipm*_*mcc 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;
        }
Run Code Online (Sandbox Code Playgroud)

那应该解决它.

  • uchuugaka的评论不正确.在case:label之后,您不仅限于单个语句.案例标签指示执行应该从哪里开始,并且继续执行直到switch语句结束(或者直到遇到中断).您不能在案例标签后面放置声明. (4认同)