我正在阅读关于AVCaptureSession等的示例代码(AVCam)。我注意到以下行(link to the code):
// In iOS 9 and later, the userInfo dictionary contains information
// on why the session was interrupted.
if ( &AVCaptureSessionInterruptionReasonKey ) {
...
}
代码中的注释是有道理的。但是对我来说没有意义的是我们为什么要获得AVCaptureSessionInterruptionReasonKey
的地址。它定义如下(在AVCaptureSection.h中):
AVF_EXPORT NSString *const AVCaptureSessionInterruptionReasonKey NS_AVAILABLE_IOS(9_0);
如果定义了密钥,其地址将如何nil
?如果没有定义这个键,代码将永远不会被编译,对吧?有人可以向我解释这个if语句是如何工作的吗?
答案 0 :(得分:3)
AVCaptureSessionInterruptionReasonKey
was added in iOS 9.0. Such an if
statement is needed only if your app also supports iOS 8 or earlier.
When the code is run on a device with iOS 9 or later, the value will be non-nil and the if
statement will be true. On devices with iOS 8 or earlier, the value will be nil
and the if
statement will be false.
If your app only supports iOS 9 or later, there is no need for the if
statement.
Read the SDK Compatibility Guide in the iOS docs for more details on this type of check.