Q:

Integer value of NSPopUpButton Selected Item

I'm trying to convert a string value of a selected item in a NSPopUpButton to it's int value by doing this (the items in the popup are a variety of numbers):
txtUPs = [txtUPs stringByAppendingString:btnUPs.titleOfSelectedItem];
numUPs = (int) txtUPs;
NSLog(@"%i" ,numUPs);

Instead of getting the integer 6 (when I select 6 from the popup) I get a huge number like 26376. What am I doing wrong?
Thanks,
Keith

A:

You should use a number formatter for this. It will handle the transformation in a localized way. (The string is presented to the user, so should be localizable.)
NSString *stringValue = [txtUPs stringByAppendingString:btnUPs.titleOfSelectedItem];
NSNumberFormatter *formatter =[[NSNumberFormatter alloc] init];

// Configure the formatter, if you need to
formatter.numberStyle = NSNumberFormatterDecimalStyle
…

// Convert it
NSNumber *numberValue = [formatter numberFromString:stringValue];
int value = [numberValue intValue];

BTW: The approach to get the internal program value from the displayed value might break: Think of items like "> 1000" or value representations that cannot be converted by a number formatter, i. e. "10k5".

