Swift NSPopUpButton枚举

时间:2019-02-04 13:40:32

标签: swift macos cocoa swift4.2 nspopupbutton

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

enter image description here

而且,我有下面的代码可以实际工作:

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)")
}

有效,但是...我相信这种实现并不那么优雅。什么是更好的方法呢?

我牢记以下要求:

  • 枚举列表“枚举重要性:Int8”的对应值是固定的。例如,EXTREMELY_IMPORTANT必须为5,因为它已经在服务器端进行了编码。因此,基于用户的选择,必须将相应的枚举值发送到服务器。 (EXTREMELY_IMPORTANT == 5,等等)

  • 此外,NSPopUpButton的选择的索引不能用于发送到服务器。例如,“非常重要”将为0,因为它是列表顶部的第一个。

  • NSPopUpButton正在使用“ titleOfSelectedItem”,然后调用getImportance(importanceEnglish:String)方法,该方法效率低下,最好改用“ indexOfSelectedItem”。这意味着,使用“非常重要”的选择索引(为0)来检索值5以发送到服务器会更有效。

  • 更好的是,如果所有内容都可以使用标准做法来支持本地化(更多语言:日语等)。

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

2 个答案:

答案 0 :(得分:2)

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

这个想法显然不是唯一的方法-还有许多其他的改动/解决方案可能与此一样(甚至可能更好)。


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 = $0.friendlyName else { return false }
            return friendlyName == name
        }) else { return nil }

        self = importance
    }

    static var allCasesNames: [String] {

        return Importance.allCases.compactMap { $0.friendlyName }
    }
}

答案 1 :(得分:1)

您可以创建带有标题和重要性作为标签的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)")
}