iOS中switch-case的優化用法


之前使用switch-case的時候一直無法使用聲明語句,只能使用調用函數的語句,今天看到了高手使用

其實也就是加一個 { } 即可。

來自於ATMHud

 

- (void)advancedHudActionForRow:(NSUInteger)row {
    [hud setBlockTouches:YES];
    switch (row) {
        case 0:
            [hud setCaption:@"This HUD will auto-hide in 2 seconds."];
            [hud show];
            [hud hideAfter:2.0];
            break;
            
        case 1:
            [hud setCaption:@"This HUD will update in 2 seconds."];
            [hud setActivity:YES];
            [hud show];
            [self performSelector:@selector(updateHud) withObject:nil afterDelay:2.0];
            break;
            
        case 2: {
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
            [hud setCaption:@"Performing operation..."];
            [hud setProgress:0.08];
            [hud show];
            break;
        }
            
        case 3: {
            ATMHudQueueItem *item = [[ATMHudQueueItem alloc] init];
            item.caption = @"Display #1";
            item.image = nil;
            item.accessoryPosition = ATMHudAccessoryPositionBottom;
            item.showActivity = NO;
            [hud addQueueItem:item];
            [item release];
            
            item = [[ATMHudQueueItem alloc] init];
            item.caption = @"Display #2";
            item.image = nil;
            item.accessoryPosition = ATMHudAccessoryPositionRight;
            item.showActivity = YES;
            [hud addQueueItem:item];
            [item release];
            
            item = [[ATMHudQueueItem alloc] init];
            item.caption = @"Display #3";
            item.image = [UIImage imageNamed:@"19-check"];
            item.accessoryPosition = ATMHudAccessoryPositionBottom;
            item.showActivity = NO;
            [hud addQueueItem:item];
            [item release];
            
            [hud startQueue];
            [self performSelector:@selector(showNextDisplayInQueue) withObject:nil afterDelay:2];
            break;
        }
    }
}

其實也就是添加了大括號

case 2: {
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
            [hud setCaption:@"Performing operation..."];
            [hud setProgress:0.08];
            [hud show];
            break;
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM