Swift System Version Checking

While it would be inaccurate to say Swift has “no C,” that’s due to swift’s lack of resemblance to target C, not lack of C.

Swift certainly draws inspiration from Haskell, Rust, Python, D, and other modern languages, but this language is perhaps best understood because it rejects all the flaws of C:

  • By default, C is not secure. By default, Swift is safe * (hence the naming of pointer manipulation functions) *.

  • C has undefined behavior. Swift has well-defined behavior.

  • C uses preprocessor instructions that are unspeakably evil. Swift has a secure subset of preprocessor instructions.

One could even say that Swift’s type system was designed specifically for C++.

In target C, checking API availability is done with a combination of C preprocessor instructions, conditions on classes, responses to selector:, and instance responses to selector: :

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000if ([NSURLSession class] && [NSURLSessionConfiguration respondsToSelector: @ the selector (backgroundSessionConfigurationWithIdentifier:)]) {... }#endifCopy the code

However, as mentioned earlier, Swift’s compiler instructions ** are extremely limited and only allow compiler flags and conditional compilation for specific operating systems, architectures, and language versions ** :

#if DEBUG     println("OTHER_SWIFT_FLAGS = -D DEBUG")#endif
Copy the code

FunctionValid Argumentsos()macOS, iOS, watchOS, tvOS, Linuxarch()x86_64, arm, arm64, i386swift()>= followed by a version number

#if os(iOS) var image: UIImage? #elseif os(macOS) var image: NSImage? #endifCopy the code

Unfortunately, OS () ‘provides any insight into a particular version of MAC OS or ios, which means you have to check at run time. Also Swift is less tolerant of zero manipulation, and checking constants for target C style causes a crash.

So how do you check the system version in Swift to determine the availability of the application programming interface? Read on to find out.

Process information

Recognizing the need for a Swift-friendly API to determine API versions at runtime, ios 8 introduced operating system version attributes and operating system minimum methods on Process Info. Both apis use a new operating system version value type that includes major, minor, and patched versions.

Apple software releases follow ** semantic version control ** conventions.

The operating system

For a simple check, such as the app “running on ios 9? Is the most direct way.

if ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)) {print("iOS >= 9.0.0")}Copy the code

Operating System Version

For more complex version comparisons, you can check the operating system version directly. Combine this with Swift pattern matching and switching statements for syntax brevity:

let os = ProcessInfo().operatingSystemVersionswitch (os.majorVersion, os.minorVersion, os.patchVersion) {case (8, 0, _) : println (" iOS 8.0.0 = >, < 8.1.0 ") case (8, _, _) : println (" iOS 8.1.0 = >, < 9.0 ") case (9, _, _) : Println (" iOS > = 9.0.0 ") default: // This code will have already crashed on iOS 7, so >= iOS 10.0 println("iOS >= 10.0.0")}Copy the code

UID evice system Version

As an alternative, you can use the system Version attribute UID EVice:

The switch UIDevice.current.systemVersion.com pare said (" 8.0.0, "options: numeric) {case. OrderedSame,. OrderedDescending: Print ("iOS >= 8") case-orderedAscending: print("iOS < 8.0")}Copy the code

Use string.compare options. Numic to ensure that, for example, “2.5” < “2.10” when comparing version strings.

String comparisons and comparison results’ are not as sexy as proprietary value types like operating system versions, but it still gets the job done.

The NSA ppKit version

Another way to determine the availability of an API is to check the framework version number. Unfortunately, the Foundation’s NS Foundation Version Number and the Core Foundation’s kCF Core Foundation Version Number are historically outdated and lack constants from past operating system versions.

This is a dead end for ios, but MAC OS can very reliably check the Version of AppKit using NS AppKit Version:

If NSAppKitVersion. Current. RawValue > =. MacOS10_10. RawValue {println (" macOS > = 10.10 ")}Copy the code

You can also remove it if you pair it with an extension to make NS AppKit Version comparable.

To summarize, here’s what you need to know about checking system versions in Swift:

  • Distinguish between ios (UI Kit) and MAC OS (AppKit) targets using the #if OS (ios) preprocessor instruction.

  • For minimum deployment targets of ios 8.0 or later, use the Process Info operating system version or is operating system at least.

  • For a minimum deployment target of ios 7.1 or below, use the compare and string on UID Evice System Version. Compare options. Numbers.

  • For MAC OS deployment targets, compare the NS AppKit Version with the available AppKit constants.