Conditional Imports in Swift

#if canImport(module) still does not solve conditional import statement in Swift 4.1?

you need to include the called functionality of your IceCream framework also with that macro like

#if canImport(IceCream)
let iceCream = IceCream()
let text = iceCream.toString()
#else
// and now?
#endif

And you should think about the else code.

Conditionally import a framework (such as Speech) based on iOS Version in Swift?

You can make the Framework optional (details and image from Ray Wenderlicht):

Making a framework optional

This, combined with your use of @available, should prevent the system from trying to load it on the devices where it is not available.

How can I conditionally depend on a system library for desktop vs iOS?

Your code totally works! Additionally, you can also use:

#if canImport(AppKit)
#endif

Or even:

#if canImport(CGLFW)
#endif

It depends on what you prefer!



Related Topics



Leave a reply



Submit