Swift NSPopUpButton 枚举

Swift NSPopUpButton enum

我正在实现 NSPopUpButton(针对使用 Swift 的 macOS 应用程序),如图所示:

而且,我有以下代码,它确实有效:

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
}

}

每当用户选择弹出菜单中的项目时,此代码就会执行:

    @IBAction func handleImportancePopUpButtonSelectionChanged(_ importancePopUpButton: NSPopUpButton) {
    let importanceIndex = getImportance(importanceEnglish: importancePopUpButton.titleOfSelectedItem!)
    print("importanceIndex: \(importanceIndex)")
}

它有效,但是......我认为这个实现不是那么优雅。这样做的更好方法是什么?

我有这些要求:

如何让我的 Swift 代码更漂亮?

我会稍微更改封装以使其更具可读性;在我看来,这样的解决方案将是一个更好的开始方式,(例如添加本地化或通过新值扩展它等...).

这个想法显然不是唯一的方法——还有许多其他 alterations/solutions 可以和这个一样好(甚至更好)。


Swift 4.2

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 = [=10=].friendlyName else { return false }
            return friendlyName == name
        }) else { return nil }

        self = importance
    }

    static var allCasesNames: [String] {

        return Importance.allCases.compactMap { [=10=].friendlyName }
    }
}

您可以创建 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)")
}