简体   繁体   中英

How to get a value from a combo box, match it to a key in a dictionary and update the value for the key from a text field?

I have a combo box with some numbers inside based in the length of the array which is the argument. I take that array and set the keys of the dictionary. My question is how to get what number the user has selected from the combo box, match it to the key in the dictionary and update the value from a different text field. I have so far set the keys but I'm stuck at the next step. Please see below the code listing. Thank you in advance.

class SetNumberOfRoomsForFloorVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {

//MARK: - Properties

@IBOutlet private weak var floorBox: NSComboBox!

@IBOutlet private weak var numberOfRoomsTxtField: NSTextField!

private var floorBoxData = [String]()

private var roomsForFloor = [String: String]()

//MARK: - Actions

override func viewDidLoad() {
    super.viewDidLoad()
    floorBoxData = representedObject as! [String]
    floorBox.usesDataSource = true
    floorBox.delegate = self
    floorBox.dataSource = self
    setTheKeys(floorsValue: floorBoxData)
    print("\(roomsForFloor.keys.sorted())")
}

private func setTheKeys(floorsValue: [String]) {
    for i in 0...floorsValue.count - 1 {
        roomsForFloor[String(i)] = ""
    }
}

@IBAction private func setRoomsForFloor(_ sender: NSButton) {
    var selectedKey: Int?
    if roomsForFloor.keys.contains(String(floorBox.indexOfSelectedItem)) {
        selectedKey = floorBox.indexOfSelectedItem
        roomsForFloor[String(describing: selectedKey)] = numberOfRoomsTxtField.stringValue
        print("\(roomsForFloor)")
        for (key, value) in roomsForFloor {
            print("\(key) + \(value)")
        }
    }
}


//MARK: - Data source

func numberOfItems(in comboBox: NSComboBox) -> Int {
    return floorBoxData.count
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
    return floorBoxData[index] // give me the item in the array at the index path.
}

}

Done it. you were right, the solution is actually very simple:

let key = String(floorBox.indexOfSelectedItem)
    let value = numberOfRoomsTxtField.stringValue
    roomsForFloor.updateValue(value, forKey: key)
    print("\(roomsForFloor)")

I get the index from the combo box and what the user puts into the text field and just add to the dictionary. So it is expecting a value for a key! Thank you for the help.

Assuming your dictionary looks like this:

"1","10"
"2","15"
"3","15"

Then there is little to do. You have the combo wired to an outlet, so all you need to do is wire it to an IBAction and inside read its stringValue . Since your roomsForFloor is [String:String] you can get the number of rooms simply by:

let theRoomsOnTheSelectedFloor = roomsForFloor[floorBox.selectedString]

I'm not clear why you would store these as strings though, that seems very odd. I also do not understand your declaration for roomsForFloor, you want an array of dictionaries? That does not make sense, unless I misunderstand that syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM