Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - “Use of undeclared type xxx” on deployment targets below or equal to iOS 10

Tags:

ios

swift

swiftui

When archiving/building the app which uses SwiftUI for platforms <= iOS 10, the compiler throws errors "Use of undeclared type".

This happens even if the enclosing type is marked as @available(iOS 13.0, *) and also using #if canImport(SwiftUI).

SwiftUI framework is also weakly linked.

Everything works fine if you are running(debug) on an iOS 11+ device, or archiving for a target with minimum supported version <= iOS 11.

enter image description here

like image 273
Kunal Verma Avatar asked May 22 '20 12:05

Kunal Verma


1 Answers

This failure occurs because builds with a deployment target earlier than iOS 11 will also build for the armv7 architecture, and there is no armv7 swiftmodule for SwiftUI in the iOS SDK because the OS version in which it was first introduced (iOS 13) does not support armv7 anymore.

I have managed to solve the archiving issue by wrapping the SwiftUI Code/Files in the preprocessor of #if arch(arm64).

Example -

#if arch(arm64)
@available(iOS 13.0, *)
struct MyCustomView: View {
    var myStrings: [String]
    var body: some View {
        List {
            ForEach(myStrings) { str in
                Text(str)
            }
        }
    }
}
#endif

This does disable previews in cases if your deployment target is <= iOS 10. But if used only when archiving, this does work.

If anyone knows of a better solution. Please share.

Adding this answer so that someone in my situation can atleast make it work with SwiftUI.

Cheers!

like image 178
Kunal Verma Avatar answered Sep 22 '22 23:09

Kunal Verma