Re: responding to NSStepper clicks
Re: responding to NSStepper clicks
- Subject: Re: responding to NSStepper clicks
- From: Quincey Morris <email@hidden>
- Date: Fri, 16 Dec 2011 20:48:03 -0800
On Dec 16, 2011, at 20:04 , Koen van der Drift wrote:
> It's not working yet. Whenever I type in the NSTextField, the number shows up twice, eg if I type '6', I see '66'. And I get the message below in the debugger console.
Um, it's not just "a message", it's an exception. Your app crashed -- crashed nicely, that's all. Until you've fixed the exception, you don't know if the misbehavior of the text field is significant.
> Is there some (Apple) sample code that shows how to use a NSTextField/NSStepper combination bound to an integer value?
You have a bug in your code. It's subtle, in the sense that it's not obvious until you realize what's going on. Then it's obvious.
When you change the value of a control that's bound to a property, the new value is an object that's set via KVC. When the property happens to be of scalar type, the object needs to be converted. This is handled by the KVC implementation, and you can see this happening in this part of your backtrace:
> 5 Foundation 0x00007fff939f4e7c _NSSetUnsignedLongLongValueForKeyWithMethod + 56
> 6 Foundation 0x00007fff939a3ded _NSSetUsingKeyValueSetter + 177
> 7 Foundation 0x00007fff939a38ad -[NSObject(NSKeyValueCoding) setValue:forKey:] + 400
> 8 Foundation 0x00007fff939d5bb2 -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 349
When the control is a text field, and the text field has a number formatter, the value object is a NSNumber. When the text field has a date/time formatter, the value object is a NSDate. Otherwise, the value object is a string.
In your case, the model property has type unsigned long long (or something equivalent to that). The KVC implementation uses a standard conversion method to convert the object to a scalar -- in this case 'unsignedLongLongValue'. This works great for NSNumber, but fails for NSString because NSString doesn't have a 'unsignedLongLongValue' method, only a 'longLongValue'. Hence:
> 2011-12-16 22:45:22.286 MyApp[4566:503] -[__NSCFConstantString unsignedLongLongValue]: unrecognized selector sent to instance 0x7fff7847da00
There are various solutions you might choose, depending on how you expect to handle validation errors for your text field. You can add a number formatter to the field, or you can change the property to type long long (though you then need to check for negative numbers yourself), or you can use KVC validation to substitute a NSNumber object for the NSString object.
P.S. I predict, after you fix this, you'll still get extra junk showing up in the text field. I wasn't following this thread very closely, and I don't remember if you said what you were doing at controlTextDidChange time, but I imagine you were trying to change the value of the stepper whenever a character is typed in the text field. Note this likely creates a KVC loop -- setting the stepper changes the property (again) via the binding, which changes the text field, which is in mid-edit. This may not be your exact scenario, but I suspect it's something along that line.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden