DEV Community

Quang
Quang

Posted on • Edited on

3

Compiler flags for SwiftUI codes

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
Enter fullscreen mode Exit fullscreen mode

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")
  }
}
Enter fullscreen mode Exit fullscreen mode

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

Imgur

But then lowering your deployment target to iOS 10 or lower and compiling to Generic iOS Device or archiving, this happens:

Imgur

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay