[swift-evolution] [Proposal] Make optional protocol methods first class citizens

Rob Mayoff mayoff at dqd.com
Fri Apr 1 20:20:58 CDT 2016


>
>
>         class UITableView {
>                 ...
>                 private func addRow(at indexPath: NSIndexPath) {
>                         ...
>                         cell.size.height = delegate?.tableView(self,
> heightForRowAtIndexPath: indexPath) ?? rowHeight
>                         ...
>                 }
>                 ...
>

You need not duplicate the default logic:


private class DefaultDelegate: NSObject, UITableViewDelegate { }
private let defaultDelegate = DefaultDelegate()

public class UITableView {

    private func addRow(at indexPath: NSIndexPath) {
        ...
        cell.size.height = (delegate ?? defaultDelegate).tableView(self,
heightForRowAtIndexPath: indexPath)
        ...
    }

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160401/e7f883b8/attachment.html>


More information about the swift-evolution mailing list