I have the following function from the Objective-C library in Swift project:
- (DDHotKey *)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task {
// ...
}
I'm trying to call this function from Swift via the following code:
hotKeyCenter.registerHotKeyWithKeyCode(
kVK_ANSI_V,
modifierFlags: NSEventModifierFlags.ControlKeyMask.rawValue,
task: { _ in
// ...
}
)
Unfortunately, it gives me the following error:
Missing argument for parameter 'action' in call
However, if I change kVK_ANSI_V
to any number like this
hotKeyCenter.registerHotKeyWithKeyCode(
34,
modifierFlags: NSEventModifierFlags.ControlKeyMask.rawValue,
task: { _ in
// ...
}
)
code compiles without any error.
As you know, kVK_ANSI_V
defined in the Carbon framework:
var kVK_ANSI_V: Int { get }
As you see, it has an Int
type while keyCode
parameter of the method has an unsigned short
type.
How can I cast Int
to unsigned short
? Is there any way to fix this error?
Thanks in advance.
You can create an UInt16
from any integer n
simply with UInt16(n)
. In your case:
hotKeyCenter.registerHotKeyWithKeyCode(
UInt16(kVK_ANSI_V),
modifierFlags: NSEventModifierFlags.ControlKeyMask.rawValue,
task: { _ -> Void in
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句