On Jun 4, 2016, at 07:10 , Daryle Walker <dary...@mac.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to