如何为不同的组合框设置不同的数据源?

How to set different data source for different combo box?

我有 3 个数组和 3 个组合框,如何将 3 个不同的数组设置为 3 个不同组合框的数据源 我知道要为 1 个组合框设置数据源,我们需要这样做

class class1: NSViewController, NSComboBoxDataSource {
@IBOutlet weak var combobox1: NSComboBox!
@IBOutlet weak var combobox2: NSComboBox!
@IBOutlet weak var combobox3: NSComboBox!

var array1 = ["1", "2", "3"]
var array2 = ["4", "5", "6"]
car array3 = ["7", "8", "9"]

override func viewDidLoad() {
    super.viewDidLoad()
    combobox1.usesDataSource = true
    combobox1.dataSource = self
    
    combobox2.usesDataSource = true
    combobox2.dataSource = self
    
    combobox3.usesDataSource = true
    combobox3.dataSource = self
}

    func numberOfItems(in comboBox: NSComboBox) -> Int {
    return array1.count
}

func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
    return array1[index]
} }

我知道这会将 array1 的值设置为 combobox1,但如何将 array2 设置为 combobox2 并将数组 3 设置为 combobox 3?

试试这个:

func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? 
{
   if comboBox == comboBox1 { return array1[index] }
   else if comboBox == comboBox2 { return array2[index] }
   else if comboBox == comboBox3 { return array3[index] }
   return nil
}