I have a class that only works on iOS 10.0 and up. It's in a Swift package, so Xcode 11 is trying to compile it for macOS too, and complaining. How do I say that it's available on iOS 10.0 only.
These don't work:
@available(iOS 10.0, *)
class Thing { ... }
@available(macOS, unavailable)
@available(iOS 10.0, *)
class Thing { ... }
Some docs: https://docs.swift.org/swift-book/ReferenceManual/Attributes.html
From docs
iOS iOSApplicationExtension macOS macOSApplicationExtension watchOS watchOSApplicationExtension tvOS tvOSApplicationExtension swift You can also use an asterisk (*) to indicate the availability of the declaration on all of the platform names listed above.
So * in @available(iOS 10.0, *) saying that declaration is available in all the other platforms. But we are also specifying that it is not available in macOS. So in the end compiler gets confused and availability wins the war. Instead of using @available you can use compiler flags,
#if os(iOS)
#endif
to specify the compiler to compile code only on iOS.
Wrap it inside this
#if canImport(UIKit)
// iOS Only code
#endif
This way, the code is excluded from compilation when the target platform doesn’t support UIKit.
You can also check with the OS type directly using #if os(iOS) or #if !os(macOS), BUT! ask yourself: why do you care about the OS? Is it because you’re using something specific to iOS like UIKit? If so, use canImport(UIKit). If not, then be more general and limit platforms using the OS check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With