I have a combobox in a View Controller that I am trying to populate using an enum. I did some research and think I've done it correct but whenever the view controller launches, I get an error Illegal NSComboBox data source. I thought it may be an issue with the ComboBox (It was copied from another one in the View Controller) so I tried creating one from scratch but I am getting the same thing. I can populate the ComboBox using the addItem no problem. The Combobox does have Use Data Source selected in the Storyboard. Here is my code;
enum Server_Locations: String, CaseIterable {
case us = "United States"
case canada = "Canada"
case other = "Other"
}
class PreferenceController: BaseVC, NSComboBoxDelegate, NSComboBoxDataSource {
@IBOutlet weak var countryComboBox: NSComboBox!
override func viewDidLoad() {
super.viewDidLoad()
countryComboBox.dataSource = self
countryComboBox.delegate = self
numberOfItemsInComboBoxCell(aComboBox: countryComboBox)
comboBoxCell(aComboBox: countryComboBox, objectValueForItemAtIndex: 0)
}
func numberOfItemsInComboBoxCell(aComboBox: NSComboBox) -> Int {
return Server_Locations.allCases.count
}
func comboBoxCell(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
return Server_Locations.allCases[index].rawValue as AnyObject
}
It seems pretty straightforward but I'm obviously doing something wrong? I think the issue is in the calls to numberOfItemsInComboBoxCell
and comboBoxCell
I've checked some examples online and they have referencing outlets to the delegate and the dataSource but I am defining them in the viewDidLoad().
I'm using Swift 5 in Storyboard mode with XCode 14.2