Skip to content

Instantly share code, notes, and snippets.

@perseusrealdeal
Last active April 22, 2023 06:23
Show Gist options
  • Save perseusrealdeal/973548df562971ecb4d9d525aa4df2ba to your computer and use it in GitHub Desktop.
Save perseusrealdeal/973548df562971ecb4d9d525aa4df2ba to your computer and use it in GitHub Desktop.
Swift compiler directives.
#if canImport(UIKit)
import UIKit
#elseif canImport(Cocoa)
import Cocoa
#endif
#if DEBUG
print("message of DEBUG")
#else
print("message of RELEASE")
#endif

os(iOS) / os(macOS) / os(tvOS) / os(watchOS)

#if os(iOS)
public typealias Responder = UIResponder
#elseif os(macOS)
public typealias Responder = NSResponder
#endif
if #available(iOS 15, macOS 12, *) {
   // Run code in iOS 15 or later and macOS 12 or later.
} else {
   // Fall back to earlier iOS and macOS APIs.
}
@available(iOS 10, macOS 10.15, *)
func newMethod() {
  // Only available when iOS >= 10, macOS >= 10.15.
}
#if targetEnvironment(simulator)
// Only if simulator.
#endif
#if swift(<5)
// Run code only if Swift version < 5.
#endif
#warning("Improvement is requried.")
#error("Fix is required.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment