If you're building for both iOS and macOS (and maybe for watchOS and tvOS, too), you're compiling at least twice: once for each platform. If you want different code to execute on each platform, you want a build-time conditional, not a run-time check.
Swift has no preprocessor, but it does have conditional build directives — and for the most part, they look like the C equivalent.
#if os(iOS) || os(watchOS) || os(tvOS)
let color = UIColor.red
#elseif os(macOS)
let color = NSColor.red
#else
println("OMG, it's that mythical new Apple product!!!")
#endif
You can also use build configurations to test for architecture (x86_64
, arm
, arm64
, i386
), Target environment (iOS simulator or Mac Catalyst), or -D
compiler flags (including the DEBUG
flag defined by the standard Xcode templates). Don’t assume that these things go together — Apple has announced macOS on arm64 to ship in 2020, so arm64 doesn’t imply iOS, and iOS Simulator doesn’t imply x86, etc.
See Compiler Control statements in The Swift Programming Language.
(If you want to distinguish which kind of iOS device you're on at runtime, use the UIDevice
class just like you would from ObjC. It's typically more useful and safe to look at the device attributes that are important to you rather than a device name or idiom — e.g. use traits and size classes to lay out your UI, check Metal for the GPU capabilities you require, etc.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…