Unexpected result of querying a property bound to a checkbox

I have found unexpected behavior when querying the boolean value of a property that is bound to the value of a checkbox. This is what I find:

Checkbox is unselected:

my propertyBoundToCheckbox is true --> false (expected result)
my propertyBoundToCheckbox is in {true} --> false (expected result)
my propertyBoundToCheckbox is false --> false (UNEXPECTED RESULT)
my propertyBoundToCheckbox is in {false} --> true (expected result)

Checkbox is selected:

my propertyBoundToCheckbox is false --> false (expected result)
my propertyBoundToCheckbox is in {false} --> false (expected result)
my propertyBoundToCheckbox is true --> false (UNEXPECTED RESULT)
my propertyBoundToCheckbox is in {true} --> true (expected result)

Why does comparison testing work as expected only when the boolean true or false value is coded as a list item?

Incidentally, comparison testing works as expected if the property is coerced to a boolean before testing:

(my propertyBoundToCheckbox) as boolean

The problem with that approach is that a checkbox can take on the value missing value, in which case the coercion throws an error. I therefore find it more convenient to test without coercion but am surprised at having to code the boolean true or false value as a list item.

The state of a checkbox isn’t a boolean: it can be NSOffState, NSOnState, and in some cases NSMixedState. They correspond to the integers 0, 1 and -1. Even than, it’s safest to do a coercion:

my propertyBoundToCheckbox as integer = 1

I wonder if you’re confusing NSMixedState with missing value.

NSOffState, NSOnState, and NSMixedState make sense, but it seems that something funny happens in the translation of those values to the value of the property bound to the checkbox, at least for the initial state of the checkbox.

In the Xib file, I have “State” set to off and “Allows mixed” unselected. Based on that, I would expect the checkbox to have as its starting value NSOffState after the window is initialized, and yet I get the following result:

return my propertyBoundToCheckbox as integer = 1 --> results in the error: "Can't make missing value into type integer."

If I then select the checkbox and run the command again, I get the expected result:

return my propertyBoundToCheckbox as integer = 1 --> true

Likewise, if I then unselect the checkbox and run the command again, I get the expected result:

return my propertyBoundToCheckbox as integer = 1 --> false

So it seems that the checkbox is initialized to some value that translates to missing value in the bound property, but subsequently translates to either of the values NSOffState or NSOnState once the button has been clicked.

It was that initial error “Can’t make missing value into type integer.” that prompted me to experiment until stumbling on the construct that works consistently, both before and after the checkbox has been clicked:

my propertyBoundToCheckbox is in {true}

The initial state of the checkbox should be the initial value of the property. Are you setting that to 0 or 1 in the property declaration? It sounds like you might be setting it to missing value…

That is correct, I am setting the value to missing value in the property declaration. I was under the impression that a property must be declared with a value of missing value in order to be recognized by the Xib file. Is that where I am going wrong? (Perhaps I am confusing a property used for binding with a property used as an outlet?)

You only need to use missing value if you plan to connect (not bind – that’s something else) the property to a control as an outlet. For bindings, the value should be whatever you want the initial value to be.

Thank you, Shane, for quickly homing in on the solution to my problem and helping me climb a bit more up the learning curve!