递归的写法

- (void)pushViewDetailViewCtrl:(UIViewController *)deViewCtrl andView:(UIView *)view{
    UIViewController *vc = (id)view.nextResponder;
    if ([vc isKindOfClass:[UIViewController class]]) {
        [vc.navigationController pushViewController:deViewCtrl animated:YES];
    }else{
        [self pushViewDetailViewCtrl:deViewCtrl andView:view.superview];
    }
}

注意:优化

普通递归:可能栈溢出crashcode

尾随递归:把累加结果也传到下一次调用(有些编译器能够对尾随递归进行优化,不过Swift不行,因此仍然可能出现栈溢出crash)递归

相关文章
相关标签/搜索