斯威夫特NSPopUpButton枚举

问题描述 投票:0回答:2

我执行的NSPopUpButton(使用雨燕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 macos cocoa swift4.2 nspopupbutton
2个回答
2
投票

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

这个想法显然不是唯一的方法 - 还有许多其他的改变/解决方案可能是因为这是很好的(或者甚至更好)。


雨燕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
投票

您可以创建一个标题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)")
}
© www.soinside.com 2019 - 2024. All rights reserved.