I'm building a universal application with SwiftUI that has an iOS app and Share Extension as well as a macOS app and Share Extension.
There's a few places where I'm using SwiftUI attributes that are iOS-specific (example a NavigationView that has a StackNavigationViewStyle attribute) and need to provide an alternative when running on macOS.
My question is: How do I ensure that these attributes that are only available for one platform don't cause build issues for the other platform? I've tried using the #if os(iOS) compiler directive, but then I get a build error saying "Unexpected platform condition (expected 'os' 'arch' or 'swift')" when doing something like this:
There's a few places where I'm using SwiftUI attributes that are iOS-specific (example a NavigationView that has a StackNavigationViewStyle attribute) and need to provide an alternative when running on macOS.
My question is: How do I ensure that these attributes that are only available for one platform don't cause build issues for the other platform? I've tried using the #if os(iOS) compiler directive, but then I get a build error saying "Unexpected platform condition (expected 'os' 'arch' or 'swift')" when doing something like this:
Code Block swift NavigationView { ... } #if os(iOS) .navigationViewStyle(StackNavigationViewStyle()) #endif
In #if compiler directive in Swift, you need to enclosed valid statements.
As you can see, .navigationViewStyle(StackNavigationViewStyle()) is not a valid statement.
You may need to write something like this:
Or this:
As you can see, .navigationViewStyle(StackNavigationViewStyle()) is not a valid statement.
You may need to write something like this:
Code Block #if os(iOS) var body: some View { NavigationView{ ... } .navigationViewStyle(StackNavigationViewStyle()) } #else var body: some View { NavigationView{ ... } .navigationViewStyle(DoubleColumnNavigationViewStyle()) } #endif
Or this:
Code Block var body: some View { let navView = NavigationView{ ... } #if os(iOS) return navView .navigationViewStyle(StackNavigationViewStyle()) #else return navView .navigationViewStyle(DoubleColumnNavigationViewStyle()) #endif }