Keerthi Keerthi - 3 months ago 24
iOS Question

How can a class be imported in swift code based on the ios version at compile time (without setting flags in Xcode or using weak linking)?

Based on the ios version, I want to import a particular class into my class. I have to do this purely through code only (Xcode and any other manual setting will not work in my situation). Any pointers on how this can be done?

For example

#import classA

//code to ensure if ios version > 3.0 then import classB

public class myClass{
...
}

Answer Source

It cannot be done. What is imported has to be determined at compile time, because importing is a compile-time action. The iOS version is unknown until runtime. At compile time you only know the deployment target and the SDK version.

I'd recommend using the usual Swift availability-checks (Apple docs) and weak linking where necessary. Or you could raise the deployment target, if that's an option. Or you find a library that solves the problem even for the lower iOS version.