Re: Overriding custom KVC methods in Swift?
Re: Overriding custom KVC methods in Swift?
- Subject: Re: Overriding custom KVC methods in Swift?
- From: Quincey Morris <email@hidden>
- Date: Sat, 04 Jun 2016 09:44:09 -0700
- Feedback-id: 167118m:167118agrif8a:167118se7Q8nLOpz:SMTPCORP
On Jun 4, 2016, at 07:10 , Daryle Walker <email@hidden> wrote:
>
> Since the KVC protocol is informal, getting the names and/or types wrong doesn’t mean an error, but that your implementation is ignored and default handling is done.
Well, there’s the same danger in Obj-C code, too. However, the clang compiler will actually suggest property-specific KVC method signatures via autocomplete.
> func validateBody(ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>) throws {
> is this the correct signature to KVC-validate a property named “body” in Swift?
I think so. Since it’s unlikely you arrived at this by guesswork, you likely got it via autocomplete for ‘validateValueForKey’ and modifying the result. This seems like the correct thing to do. (You could also submit a bug asking for the clang autocomplete behavior.)
> (Incidentally, is that a good implementation?)
It looks OK. The only objection I would have is unrelated to validation. Instead of this code:
> if !errors.isEmpty {
> throw errors.first!
> }
I’d suggest this:
> if let firstError = errors.first {
> throw firstError
> }
Using the IUO operator (in “errors.first!”) seems to be regarded as a bit of a code smell, especially when there’s a natural alternative using optional binding. Also, the second version has one less method call, which saves the lives of a couple of electrons that would otherwise be sacrificed to your app.
_______________________________________________
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