Search code examples
iosswiftxcode6swift-playground

Playground: Use of unresolved identifier 'NSColor'


I tried to play with playground in Xcode6-Beta4, and type the following:

import UIKit
let color = NSColor.blueColor()

and the error is:

Use of unresolved identifier 'NSColor'

Can someone explain why?


Solution

  • If you are developing for iOS and thus using import UIKit (as your code shows), then the appropriate color interface comes from UIColor. As such:

    enter image description here

    For code meant to work in both iOS and OS X you can use:

    #if os(macOS) || targetEnvironment(macCatalyst)
    import AppKit
    typealias ColorType = NSColor
    #elseif os(iOS)
    import UIKit
    typealias ColorType = UIColor
    #endif
    
    let color = ColorType.blackColor()