Reputation: 16399
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
Upvotes: 3
Views: 2375
Reputation: 119128
Wrap it inside this
#if canImport(UIKit)
// iOS Only code
#endif
This way it will remove from compiled project if it's not going to iOS device.
You can check with OS type like #if os(iOS)
too, but be specific to why you should care about the OS? isn't it because of something especially for iOS like UIKit
? If not, be more general and limit the OS
as @Shreeram mentioned.
Upvotes: 4
Reputation: 3157
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.
Upvotes: 7