iOS return、break和continue的区别

2020-03-07  本文已影响0人  邓布利多教授
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    btn.center = self.view.center;
    btn.backgroundColor = UIColor.redColor;
    [btn addTarget:self action:@selector(buttonSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

-(void)buttonSelectAction{
    
    NSLog(@"开始");
    [self testMethod];
    NSLog(@"结束");
    
}

-(void)testMethod{
    
    NSLog(@"方法内部开始");
    for (int i = 0; i < 10; i ++) {
        
        if (i == 5) {
            NSLog(@"进入!");
//            return;/// 跳出当前作用域,返回到函数调用处,继续执行
//            break;/// 跳出当前代码块,继续执行
//            continue;/// 继续执行
        }
        NSLog(@"执行中...");
        
    }
    NSLog(@"方法内部结束");
    
}
上一篇下一篇

猜你喜欢

热点阅读