As an example, I'll be using a LazyVStrack
, which is available for iOS 14.0
, macOS 11.0
, tvOS 14.0
, and watchOS 7.0
.
I want to create a view, that has the same API level, but is only exposed to iOS
:
@available(iOS 14.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
struct SomeView: View {
var body: some View {
LazyVStack(content: {
EmptyView()
})
}
}
When switching to macOS
, even though SomeView
is marked as unavailable, compiler still shows error: "'LazyVStack' is only available in macOS 11.0 or newer". Why is that?
One solution is changing the availability attribute to @available(macOS 11.0, *)@available(macOS, unavailable)
. I don't know if that's the way to go.
@available(iOS 14.0, *)
@available(macOS 11.0, *)@available(macOS, unavailable)
@available(tvOS 14.0, *)@available(tvOS, unavailable)
@available(watchOS 7.0, *)@available(watchOS, unavailable)
struct SomeView: View {
var body: some View {
LazyVStack(content: {
EmptyView()
})
}
}
Another solution is wrapping the entire declaration/file under #if os(iOS)
, which defeats the purpose of the availability attributes. Plus, I can't go that way. I need it for a package, and want the object to still be visible, but not available.
#if os(iOS)
@available(iOS 14.0, *)
struct SomeView: View {
var body: some View {
LazyVStack(content: {
EmptyView()
})
}
}
#endif
Yet another solution is wrapping bodies with conditional compilation flags. This is valid, but seems excessive.
@available(iOS 14.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
struct SomeView: View {
var body: some View {
#if os(iOS)
LazyVStack(content: {
EmptyView()
})
#endif
}
}
Is this an intended behavior? If so, is there a better way of tackling this?