Objective-C @available guard AND'ed with more conditions

前端 未结 8 1310
眼角桃花
眼角桃花 2021-01-01 11:17

Objective-C has an @available expression in XCode 9+ / LLVM 5+ that allows you to guard a block of code to at least a certain OS version so that it won\'t emit unguarded ava

8条回答
  •  难免孤独
    2021-01-01 11:45

    You do what you always do when you have complex conditional code in the middle of a function that makes the flow complex: you hoist it into another function.

    - (void)handleThing {
        if (@available(iOS 11.0, *)) {
            if (some_condition) {
                // code to run when on iOS 11+ and some_condition is true
                return;
            }
        }
    
      // code to run when on older iOS or some_condition is false
    }
    

    Or you hoist the check into generic code (see Josh Caswell's; it's better than how I originally wrote this).

提交回复
热议问题