How do I set an NSButton's keyEquivalent (which is a String type) to the down arrow key in Swift? NSDownArrowFunctionKey is an Int.
Asked
Active
Viewed 1,372 times
1 Answers
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
-
1Thanks. 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