0

I am trying to get a String value to a Float value from a NSPopUpButton.

E.g. I have 3 number selections. 50, 20, 10. When the user selects one of the numbers I would like a calculation made into a Float value.

I know this may be something very simple but I am new and I can't find anything on Stackoverflow. Any help would be appreciated. Here is an example of the code I have.

@IBOutlet weak var userInput: NSTextField!
@IBOutlet weak var result: NSTextField!
@IBOutlet weak var userMenuSelection: NSPopUpButton!

override func viewDidLoad() {
    super.viewDidLoad()

    userMenuSelection.removeAllItems()
    userMenuSelection.addItems(withTitles: ["50", "20", "10"])
}

@IBAction func pushButtonforResult(_ sender: Any) {
    /* 
    Not sure how to take the selected userMenuSelection and multiply it by the users input to get my result in a float. 
    E.g. look below. This of course does not work because the menu items are strings. 
    */

    result.floatValue = userMenuSelection * userInput.floatValue
}

Hope this makes sense to what I am asking.

thisIsTheFoxe
  • 1,584
  • 1
  • 9
  • 30
Costa
  • 39
  • 6

2 Answers2

0

You can try

let arr = ["50", "20", "10"]

//

@IBAction func pushButtonforResult(_ sender: NSPopUpButton) {
   let selectedValue = Float(arr[sender.selectedTag()])!
   Result.floatValue = selectedValue * userInput.floatValue

   // OR

    let selectedValue = Float(sender.selectedItem!.title)!
    result.floatValue = selectedValue * userInput.floatValue

}
AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Hi thanks for your input. I tried both those options but they do not work in my code. Also "let" can't be used when userMenuSelection is an @IBoutlet. Any ideas? – Costa Jul 07 '18 at 22:36
  • try edit ......... also don't start a variable name with capital letter – Shehata Gamal Jul 07 '18 at 22:40
  • I think its getting closer to working but still getting an error. This is the error: Value of type 'Any' has no member 'selectedTag' Cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members. – Costa Jul 07 '18 at 23:22
  • convert the sender parameter from **Any** to **NSPopUpButton** – Shehata Gamal Jul 07 '18 at 23:23
  • ah yes that makes sense. Ok now there is another error message. its highlighting the result variable and says: Value of type 'NSTextField' has no member 'floatvalue' – Costa Jul 07 '18 at 23:44
  • Its working now. I really appreciate you taking the time. I learned a lot :) – Costa Jul 08 '18 at 03:59
  • If you don't set the tags then use `indexOfSelectedItem` instead of `selectedTag()`. – Willeke Jul 08 '18 at 10:34
0

As you see, your userMenuSelection has titles representing the Float values as String.

You can simply retrieve the selected title and convert it to Float:

@IBAction func pushButtonforResult(_ sender: Any) {
    var selectedValue = Float(userMenuSelection.titleOfSelectedItem ?? "0") ?? 0.0

    result.floatValue = selectedValue * userInput.floatValue
}
OOPer
  • 47,149
  • 6
  • 107
  • 142