简体   繁体   中英

iOS 11.1 feature in Xcode 9.0

(This is based on an issue here: https://github.com/dokun1/Lumina/issues/44 )

Consider the following function:

fileprivate var discoverySession: AVCaptureDevice.DiscoverySession? {
    var deviceTypes = [AVCaptureDevice.DeviceType]()
    deviceTypes.append(.builtInWideAngleCamera)
    if #available(iOS 10.2, *) {
        deviceTypes.append(.builtInDualCamera)
    }
    if #available(iOS 11.1, *), self.captureDepthData == true {
        deviceTypes.append(.builtInTrueDepthCamera)
    }
    return AVCaptureDevice.DiscoverySession(deviceTypes: deviceTypes, mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
}

I am running Xcode 9.0. I want to run a framework that makes use of this feature in iOS 11.1, which is only available in Xcode 9.1. The code in this function that gives an error is:

if #available(iOS 11.1, *), self.captureDepthData == true {
    deviceTypes.append(.builtInTrueDepthCamera)
}

When running on Xcode 9.1 on someone else's machine, this works fine, and the application developing with this framework can set a development target of 10.0, and it compiles fine. However, I can't even build the framework on my machine. The error I get reads Type 'AVCaptureDevice.DeviceType' has no member 'builtInTrueDepthCamera' in Xcode 9.0 I thought using the #available macro would fix this, but it doesn't work that well.

I've also tried to use this:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 111000
    if #available(iOS 11.1, *), self.captureDepthData == true {
        deviceTypes.append(.builtInTrueDepthCamera)
    }
#endif

But this causes an error reading: Expected '&&' or '||' expression Expected '&&' or '||' expression

Anyone know what to do?

#available will raise the "SDK Level" so that the compiler will allow you to use API calls above your Deployment target, but it won't prevent the compiler from compiling the lines inside the #available scope.

You need to prevent the compiler from compiling those lines because the compiler doesn't have a definition for .builtInTrueDepthCamera. You can do that using the #if build configuration statement.

In this case you want to check for swift version 4.0.2. Xcode 9.1 shipped with Swift 4.0.2.

#if swift(>=4.0.2)
  if #available(iOS 11.1, *), self.captureDepthData == true {
    deviceTypes.append(.builtInTrueDepthCamera)
  }
#endif

source: https://www.bignerdranch.com/blog/hi-im-available/#what-it-is-not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM