Macos 使用绑定和结构时的自定义nspopupbutton标题

Macos 使用绑定和结构时的自定义nspopupbutton标题,macos,cocoa,timezone,cocoa-bindings,nspopupbutton,Macos,Cocoa,Timezone,Cocoa Bindings,Nspopupbutton,如何使用绑定(使用结构)显示时区标识符?e、 g.不是非洲/xxx偏移3600,我只想要非洲/xxx class TimezonePopupButton: NSPopUpButton { private var timezonesController: NSArrayController = NSArrayController() private var timeZones : [TimeZone] = { var content : [TimeZone] =

如何使用绑定(使用结构)显示时区标识符?e、 g.不是非洲/xxx偏移3600,我只想要非洲/xxx

class TimezonePopupButton: NSPopUpButton {

    private var timezonesController: NSArrayController = NSArrayController()

    private var timeZones : [TimeZone] = {
        var content : [TimeZone] = []
        for identifier in TimeZone.knownTimeZoneIdentifiers {
            if let timeZone = TimeZone(identifier: identifier) {
                content.append(timeZone)
            }
        }
        return content
    }()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() {
        timezonesController.content = timeZones
        bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
        selectItem(withTitle: TimeZone.current.identifier)
    }
}

PS:用这个黑客可以做到,但它看起来非常不快,我担心它将来会崩溃

extension NSTimeZone {

    open override func value(forKey key: String) -> Any? {
        if key == "description" {
            return self.name
        }
        return super.value(forKey: key)
    }
}

绑定
contentValues

在NSPopUpButton中显示为项目的字符串数组


请参阅。

只是一个问题。这是因为objc运行时是NSTimezone,因此响应名称。当swift时区使用“标识符”时,它能证明未来吗(目前它崩溃了)对不起,我不明白这个问题。我的错。意味着绑定使用结构时区;此结构没有“名称”作为属性/变量。它之所以能够工作,是因为struct TimeZone位于NSTimeZone之下,具有一个名为“name”的属性。作为objc-dev,我希望“identifier”变量是可绑定的(不是)。我只是担心,如果新版本的macOS或swiftI会使此代码崩溃,我认为
name
属性与
时区
NSArrayController
和绑定一起使用一样,是未来的证明。
bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
bind(.contentValues, to: timezonesController, withKeyPath: "arrangedObjects.name")