Re: KVC Validation w/ custom Formatter not working
Re: KVC Validation w/ custom Formatter not working
- Subject: Re: KVC Validation w/ custom Formatter not working
- From: Luc Van Bogaert <email@hidden>
- Date: Fri, 03 Mar 2017 09:11:04 +0100
The formatting with the custom formatter is working. Due to a misunderstanding on my part, I was incorrectly assuming that the validation method was not called. Actually, that is only the case when the formatter returns "false" from its getObjectValue(:for:errorDescription:) method, which I guess is by design. On the contrary, when the formatter decides the input is valid, my validation method indeed gets called.
Thanks for your suggestion on using a custom validation method.
--
Luc Van Bogaert
Follow my Blog @ http://folkwire.wordpress.com
> On 27 Feb 2017, at 22:58, Quincey Morris <email@hidden> wrote:
>
> On Feb 26, 2017, at 12:40 , Luc Van Bogaert <email@hidden <mailto:email@hidden>> wrote:
>>
>> my model class overrides
>>
>> validateValue(_ ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey inKey: String) throws
>>
>> After changing the text in the textfield, my validation method is not called. When I replace my custom formatter with an instance of NSNumberFormatter, everything works as expected.
>
> You don’t say whether your custom formatter is used for formatting, and it’s just validation that’s missing, or if the formatter is totally ignored. It might be worth adding code that checks to see if the field has a formatter, and if the formatter has the correct class. Or, if the NIB loading calls a different initializer than you were expecting, then perhaps your formatter isn’t being set up properly.
>
> I have a vague recollection that there was an IB bug where custom formatters were not honored, which was fixed in a recent version of Xcode. However, I can’t find anything about this in the release notes, so I may have dreamed it. Still, it might be worth trying this in the latest Xcode beta.
>
> Also, I would not recommend overriding validateValue(_:forKey:) directly, since that overrides the entire KVC validation mechanism. That shouldn’t be a problem if you invoke the super implementation properly for keys you don’t handle, but it’s probably safer and clearer to use a custom validation method of the form validate<key>(_:). See:
>
> https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/ValidatingProperties.html <https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/ValidatingProperties.html>
>
>
_______________________________________________
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