0

I wanted to use an NSButton's integerValue to pass on information in a method and ran into a curious bug: whatever I set a value to - whether integerValue, stringValue, or floatValue, when I read it out, it's 1.

NSButton inherits from NSControl and should have the usual set of values... but all of them, including strings, resolve to 1.

Nothing I can see in NSControl.h or NSButton.h points to xxValue being anything other than ordinary properties.

I'm working in Xcode 9/Swift 4/macOS 10.12.6 - is this a known problem? Is this documented anywhere? I've used .tag as a workaround but would prefer not to.

green_knight
  • 1,319
  • 14
  • 26
  • 1
    Don't use the integer value of a *control* to retrieve information, it's highly bad practice. – Tamás Sengel Nov 25 '17 at 23:42
  • "whatever I set a value to - whether integerValue, stringValue, or floatValue, when I read it out, it's 1. " Nobody sees that since you don't show a single line of work. – El Tomato Nov 25 '17 at 23:58
  • @the4kman I wasn't going to, but I needed a quick way to track a sender in something I was testing out, and changing the tag of a button dynamically seemed even _more_ suspicious, conceptually (but did the trick in helping me track down my bug. Sometimes I just want a diagnostic payload). And once I stumble across an oddity, I try to work out where it's coming from and how I could have found out. – green_knight Nov 26 '17 at 09:53

1 Answers1

4

It’s not a bug.

From the documentation of NSButton:

For most types of buttons, the value of the button matches its state—the value is 1 for on, 0 for off, or -1 for mixed. For pressure-sensitive buttons, the value of the button indicates pressure level instead.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thank you - I *knew* I was missing something, I just could not work out what. I obviously didn't read the introductory text closely enough. Riddle solved. – green_knight Nov 26 '17 at 09:47