How to display timezone identifier with bindings (using structs)? e.g. instead of Africa/xxx offset 3600 i want Africa/xxx only
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: It can be done with this hack but it looks very non swifty and i am afraid it will break in future
extension NSTimeZone {
open override func value(forKey key: String) -> Any? {
if key == "description" {
return self.name
}
return super.value(forKey: key)
}
}
Bind contentValues
An array of strings that are displayed as the items in the NSPopUpButton.
bind(.content, to: timezonesController, withKeyPath: "arrangedObjects")
bind(.contentValues, to: timezonesController, withKeyPath: "arrangedObjects.name")