Wildcard_Checks

Wildcard checks

Intro

Wildcard checks allows the consumer of the API to cope with breaking changes.

Mind Map

available

deprecations

Get Device Info

check Library

Checking if specific library is supported on the OS running the code or framework right now.

#if canImport(UIKit)
// iOS, tvOS, and watchOS – use UIColor
import UIKit
print("This code only runs if UIKit is supported on the device OS.")

#elseif canImport(AppKit)
// macOS – use NSColor
#else
// all other platforms – use a custom color object
#endif

HWS | canImport

Weak linking framework

Using optional libraries requires additional code as it makes use of weak linking

extern int MyWeakLinkedFunction() __attribute__((weak_import));

-weak_framework <framework_name>

if (MyWeakLinkedFunction != NULL)
{
    result = MyWeakLinkedFunction();
}

check OS

Why do you maybe need this.
Well if you are writing a framework or library and you started out with supporting both macOS and iOS. But while running swift DocC publisher you can try to avoid building errors for macOS target.

I encountered this problem while running https://github.com/SensehacK/swift-sense for this Swift DocC.

#if os(macOS)
func someMacOSOnlyFunction() { /* ... */ }
#endif
#if os(iOS)
import UIKit
public extension Image { }
#endif

In SwiftUI you can use this for limiting the UI as well.

NavigationLink(value: asset) {
	CustomView(asset: asset)
}
#if os(iOS)
.listRowSeparatorTint(.black)
#endif

#if os(iOS)
var drag: some Gesture {
	DragGesture()
}
#endif

swift forums | if-vs-available-vs-if-available

check Simulator

#if targetEnvironment(simulator)
// your code
#endif

check Xcode

Objective-C version just checking iOS 14 >*

#if __clang_major__ >= 16
extension mamba.HLSPlaylist: @unchecked @retroactive Sendable { }
#or
extension mamba.HLSPlaylist: Sendable { }
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000  __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
...
#endif

check Swift

#if swift(>=6.0)
    @objc
    var identifier: Any? {
        "sa.drm.key"
    }
#endif

#if swift(>=6.0)
override var identifier: (any Sendable)? {
	urlIdentifier
}
#else
override var identifier: Any? {
	urlIdentifier
}
#endif

check compiler

I found that #if compiler(>=5.5) works here. Note, this is different than if swift(>=5.5), which will not necessarily work depending on the swift version you have set in your project.

#if compiler(>=6.0)
    override var identifier: (any Sendable)? {
        urlIdentifier
    }
#else
    override var identifier: Any? {
        urlIdentifier
    }
#endif

I was able to determine the different Xcode, Swift & Clang version differences via the app Xcodes mentioned here