Friday, October 14, 2016 [Tweets] [Favorites]

Swifty Delegates

Soroush Khanlou:

These are horrible. Why are both of these methods called messageForm? Also, starting a method with a noun here doesn’t make sense: it usually suggests that you’ll be returning an object of that type (think about data(using:) on NSString, which returns a Data). We’re not returning any message form objects here. That “message form” is actually the name of the first parameter. These are very confusing method names!

Both of these types of delegate methods can be fixed by moving the “sender” to the back of the line, and bringing the verbs forward. For the first one, the event that the sender is informing the delegate about is didTapCancel, instead of messageFormDidTapCancel.

[…]

I haven’t found a hard and fast rule for which preposition to use yet. I’ve found “on”, “for”, “with”, and “in” to all be useful in different circumstances.

[…]

These rules aren’t endorsed by anyone except for me, but I think they make much more sense than the current rules by which we write delegate methods. Going forward, I’ll probably start writing my Swift delegate methods with this structure.

1 Comment

I assume (*) that having the delegate owner as the first parameter in Obj-C was mainly to have a consistent location for this parameter. Because otherwise the names of the Obj-C do not also make any sense.

* Couldn't find the header for NXTableView to see whether there could have been an obvious reason related to the original interface.

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment