SwiftUI is a very interesting library introduced at WWDC 2019. It brings iOS developer community a novel way to develop UIs for iOS and MacOS apps.
SwiftUI is also the first Swift binary framework that Apple introduced despite Swift was born in 2014 and been very popular.
To integrate SwiftUI, simply importing it, right?
import SwiftUI
First of all, SwiftUI is only available on iOS 13 and later. It's likely that your app or team's app has to support iOS 12, 11. So adding @available
is needed.
@available(iOS 13, *)
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
Next, to make sure your app doesn't crash at launch on iOS less than 13, you need to add SwiftUI as -weak-framework
, the same for Combine, which was also introduced last WWDC.
or
But then lowering your deployment target to iOS 10 or lower and compiling to Generic iOS Device or archiving, this happens:
the reason for this is because SwiftUI wasn't compiled for 32 bit architectures. So adding 64-bit compiler flag to every SwiftUI code is required.
#if (arch(x86_64) || arch(arm64))
@available(iOS 13.0, *)
struct HelloWorld : View {
var body: some View {
//...
}
}
#endif
canImport(SwiftUI)
won't help you solve the problem above.
Finally, SwiftUI uses many new Swift 5.1 features, like some
keyword of opaque return types and property wrappers. So if you, at some point, wants your code to compiles to Xcode before 11, you needs to add:
#if compiler(>=5.1)
//...
#endif
compiler
flag actually represents Xcode 11, because changing SWIFT_VERSION
in Xcode project still can get SwiftUI fully functional. So adding #if swift(>=5.1)
is not the correct way.
Top comments (0)