How to determine device type from Swift? (OS X or iOS)

后端 未结 5 729
情深已故
情深已故 2020-12-04 16:46

I know Swift is relatively new, but I was wondering if there was a way to determine the device type?

(Like you used to be able to do with a #define)?

5条回答
  •  春和景丽
    2020-12-04 17:10

    Since Swift 4.2 you can replace

    #if os(iOS) || os(watchOS) || os(tvOS)
        let color = UIColor.redColor()
    #elseif os(OSX)
        let color = NSColor.redColor()
    #else
         println("OMG, it's that mythical new Apple product!!!")
    #endif
    

    By

    #if canImport(UIKit)
        let color = UIColor.redColor()
    #elseif os(OSX)
        let color = NSColor.redColor()
    #else
        #error("OMG, it's that mythical new Apple product!!!")
    #endif
    

提交回复
热议问题