gpt4 book ai didi

swift - "The Selector keyword has been deprecated in future versions of Swift"如何在没有编辑菜单的对话框中创建键盘快捷键

转载 作者:可可西里 更新时间:2023-11-01 01:36:55 25 4
gpt4 key购买 nike

Cut/Copy/Paste/SelectAll/Undo/Redo 的 Swift 2.1 解决方案是 here ,但这现在会在 Xcode 7.3/Swift 2.2 中产生 6 个警告。 Selector 关键字在 Swift 的 future 版本中已被弃用。

这是一个部分解决方案,它编译时没有针对剪切/复制/粘贴/全选的警告:

if NSApp.sendAction(Selector("cut:"), to:nil, from:self) { return true }

成为

if NSApp.sendAction(#selector(NSText.cut(_:)), to:nil, from:self) { return true }

不过Undo/Redo还是有待解决

尝试 1:这会产生错误,因为 NSText.undo 不存在

if NSApp.sendAction(#selector(NSText.undo(_:)), to:nil, from:self) { return true }

尝试 2:编译但不起作用:

if NSApp.sendAction(#selector(NSUndoManager.undo), to:nil, from:self) { return true }

这里是完整的代码片段:

class NSTextFieldWithKeyboard: NSTextField {

private let commandKey = NSEventModifierFlags.CommandKeyMask.rawValue
private let commandShiftKey = NSEventModifierFlags.CommandKeyMask.rawValue | NSEventModifierFlags.ShiftKeyMask.rawValue
override func performKeyEquivalent(event: NSEvent) -> Bool {
if event.type == NSEventType.KeyDown {
if (event.modifierFlags.rawValue & NSEventModifierFlags.DeviceIndependentModifierFlagsMask.rawValue) == commandKey {
switch event.charactersIgnoringModifiers! {
case "x":
// New Swift 2.2 #selector works for cut, copy, paste and select all
if NSApp.sendAction(#selector(NSText.cut(_:)), to:nil, from:self) { return true }
case "c":
if NSApp.sendAction(#selector(NSText.copy(_:)), to:nil, from:self) { return true }
case "v":
if NSApp.sendAction(#selector(NSText.paste(_:)), to:nil, from:self) { return true }
case "z":
// Old method from Swift 2.1
// This functions, but produces the warning:
// No method declared with Objective-C selector 'undo:'
if NSApp.sendAction(Selector("undo:"), to:nil, from:self) { return true }

// Attempt 1: New use of Swift 2.2, but NSText.undo does not exist
// Type 'NSText' has no member 'undo'
// if NSApp.sendAction(#selector(NSText.undo(_:)), to:nil, from:self) { return true }

// Attempt 2: Swift 2.2, but NSUndoManager.undo exists and compiles, but does not work
// if NSApp.sendAction(#selector(NSUndoManager.undo), to:nil, from:self) { return true }
case "a":
if NSApp.sendAction(#selector(NSText.selectAll(_:)), to:nil, from:self) { return true }
default:
break
}
}
else if (event.modifierFlags.rawValue & NSEventModifierFlags.DeviceIndependentModifierFlagsMask.rawValue) == commandShiftKey {
if event.charactersIgnoringModifiers == "Z" {
// The same warning we get with undo, we get with redo
if NSApp.sendAction(Selector("redo:"), to:nil, from:self) { return true }
}
}
}
return super.performKeyEquivalent(event)
}
}

最佳答案

尝试 #2 无效,因为 NSUndoManager 有一个带有选择器 undo 而不是 undo: 的方法。

我建议您创建一个协议(protocol)来表示“响应撤消操作的实体”并使用该协议(protocol)来保存您的选择器:

@objc protocol UndoActionRespondable {
func undo(sender: AnyObject)
}

let undoSelector = #selector(UndoActionRespondable.undo(_:))

关于swift - "The Selector keyword has been deprecated in future versions of Swift"如何在没有编辑菜单的对话框中创建键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36156712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com