2

How do I set an NSButton's keyEquivalent (which is a String type) to the down arrow key in Swift? NSDownArrowFunctionKey is an Int.

sam
  • 3,399
  • 4
  • 36
  • 51

1 Answers1

3

This page in Apple's documentation addresses this exact issue. The example they provide is written in Objective-C, the Swift equivalent is as follows:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var button: NSButton!

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        var array = [unichar(NSDownArrowFunctionKey)]
        button.keyEquivalent = NSString(characters: array, length: 1)
    }
}
Paul Patterson
  • 6,840
  • 3
  • 42
  • 56
  • 1
    Thanks. I added `as String` to the end of the `NSString` call. Alternatively, `String(utf16CodeUnits: array, count: 1)` also seems to work. – sam Mar 31 '15 at 20:18