[英]Swift NSPopUpButton enum
I'm implementing an NSPopUpButton (for a macOS app using Swift), as in the picture: 我正在实现一个NSPopUpButton(用于使用Swift的macOS应用程序),如图所示:
And, I have the following code, which actually works: 而且,我有以下代码可以实际工作:
enum Importance: Int8 {
case EXTREMELY_IMPORTANT = 5
case VERY_IMPORTANT = 4
case IMPORTANT = 3
case NORMAL = 2
case NOT_IMPORTANT = 1
case JUST_FOR_RECORD = 0
case ERROR = -1
}
let english_extremely_important = "Extremely Important"
let english_very_important = "Very Important"
let english_important = "Important"
let english_normal = "Normal"
let english_not_important = "Not Important"
let english_just_for_record = "Just for Record"
var importanceEnglishItems: [String] = {
return [
english_extremely_important,
english_very_important,
english_important,
english_normal,
english_not_important,
english_just_for_record
]
}()
func getImportance(importanceEnglish: String) -> Int8 {
switch importanceEnglish {
case english_extremely_important:
return Importance.EXTREMELY_IMPORTANT.rawValue
case english_very_important:
return Importance.VERY_IMPORTANT.rawValue
case english_important:
return Importance.IMPORTANT.rawValue
case english_normal:
return Importance.NORMAL.rawValue
case english_not_important:
return Importance.NOT_IMPORTANT.rawValue
case english_just_for_record:
return Importance.JUST_FOR_RECORD.rawValue
default:
return Importance.ERROR.rawValue
}
}
Whenever the user selects the item in the popup menu, this code executes: 每当用户在弹出菜单中选择项目时,此代码就会执行:
@IBAction func handleImportancePopUpButtonSelectionChanged(_ importancePopUpButton: NSPopUpButton) {
let importanceIndex = getImportance(importanceEnglish: importancePopUpButton.titleOfSelectedItem!)
print("importanceIndex: \(importanceIndex)")
}
It works, BUT... I believe this implementation isn't that elegant. 可以,但是...我相信这种实现并不那么优雅。 What is the better way to do this?
什么是更好的方法呢?
I have these requirements in mind: 我谨记以下要求:
The corresponding values of the enums list "enum Importance: Int8" are fixed. 枚举列表“枚举重要性:Int8”的对应值是固定的。 For example, EXTREMELY_IMPORTANT must be 5, as it is already coded on the server-side.
例如,EXTREMELY_IMPORTANT必须为5,因为它已经在服务器端进行了编码。 Therefore, based on user's selection, the corresponding enum values must be sent to be server.
因此,基于用户的选择,必须将相应的枚举值发送到服务器。 (EXTREMELY_IMPORTANT == 5, etc.)
(EXTREMELY_IMPORTANT == 5,依此类推)
Further to the above point, the selection's index of the NSPopUpButton cannot be used for sending to the server. 除此之外,NSPopUpButton的选择索引不能用于发送到服务器。 For example, "Extremely Important" would be 0 since it is the first one on the top of the list.
例如,“非常重要”将为0,因为它是列表顶部的第一个。
The NSPopUpButton is using "titleOfSelectedItem" and then call getImportance(importanceEnglish: String) method, which is inefficient, and should be better off using "indexOfSelectedItem" instead. NSPopUpButton使用“ titleOfSelectedItem”,然后调用getImportance(importanceEnglish:String)方法,该方法效率低下,最好改用“ indexOfSelectedItem”。 That means, it would be more efficient to use the selection index of "Extremely Important" (which is 0) to retrieve the value of 5 for sending to the server.
这意味着,使用“非常重要”的选择索引(为0)来检索5值以发送到服务器会更有效。
Better yet, if everything can support support localization (more languages: Japanese, etc.) using standard practice. 更好的是,如果一切都可以使用标准实践来支持本地化支持(更多语言:日语等)。
How can I make my Swift code more beautiful? 如何使我的Swift代码更漂亮?
I would change the encapsulation a little bit to make it more readable; 我将稍微改变封装使其更具可读性。 such solution would be a better way to start with in my view, (eg adding localisation or extending it by new values, etc...) .
在我看来,这样的解决方案将是更好的方法(例如,添加本地化或通过新值扩展它等) 。
this idea is obviously not the only way – there are many other alterations/solutions could be as good as this (or maybe even better). 这个想法显然不是唯一的方法-还有许多其他更改/解决方案可能与此(甚至可能更好)一样好。
enum Importance: Int, CaseIterable {
case extremelyImportant = 5
case veryImportant = 4
case important = 3
case normal = 2
case notImportant = 1
case justForRecord = 0
var friendlyName: String? {
switch self {
case .extremelyImportant: return "Extremely Important"
case .veryImportant: return "Very Important"
case .important: return "Important"
case .notImportant: return "Not Important"
case .justForRecord: return "Just for Record"
default: return nil
}
}
init?(withName name: String) {
guard let importance = Importance.allCases.first(where: {
guard let friendlyName = $0.friendlyName else { return false }
return friendlyName == name
}) else { return nil }
self = importance
}
static var allCasesNames: [String] {
return Importance.allCases.compactMap { $0.friendlyName }
}
}
You can create NSMenuItem
with a Title and importance as tag and add it NSPopUpButton.menu.items
. 您可以创建
NSMenuItem
带有标题和重要性的标签,并将其添加NSPopUpButton.menu.items
。
override func viewDidLoad() {
super.viewDidLoad()
popUpButton.menu?.items = self.importanceEnglishItems
}
class func MenuItem(title: String, tag: Int) -> NSMenuItem {
let item = NSMenuItem(title: title, action: nil, keyEquivalent: "")
item.tag = tag
return item
}
var importanceEnglishItems: [NSMenuItem] = {
return [
MenuItem(title: "Extremely Important", tag: 5),
MenuItem(title: "Very Important", tag: 4),
MenuItem(title: "Important", tag: 3),
MenuItem(title: "Normal", tag: 2),
MenuItem(title: "Not Important", tag: 1),
MenuItem(title: "Just for Record", tag: 0)
]
}()
@IBAction func handleSelection(_ sender: NSPopUpButton) {
guard let item = sender.selectedItem else { return }
print("importanceIndex: \(item.tag)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.