Conditional Imports in Swift not working?

335 Views Asked by At

I want to use a module (SwiftyStoreKit) which has a minimum requirement of WatchOS 6.2, however I don't want to increase my WatchOS target because I don't want to lose support for older watches. My understanding is that Swift now has conditional imports which will let you import a module only in the OS allows the import but the below code is not working? I'm still getting a compiling error "Compiling for watchOS 4.3, but module 'SwiftyStoreKit' has a minimum deployment target of watchOS 6.2:" and if I remove "import SwiftyStoreKit" the module isn't detected.

#if canImport(SwiftyStoreKit)
import SwiftyStoreKit

class SwiftyStoreKitWatchHelper {

}
#endif
1

There are 1 best solutions below

1
Same7Farouk On

Try this

#if canImport(SwiftyStoreKit)
import SwiftyStoreKit
#endif

@available(watchOS 6.2)
class SwiftyStoreKitWatchHelper {
    
}