LoginSignup
8
4

More than 5 years have passed since last update.

Swiftのデリゲートメソッドでデフォルト引数ぽく書く方法

Last updated at Posted at 2019-03-11

概要

コードレビューの時にデリゲートメソッドの部分で指摘を受けて修正しようと思ったのですが、
なかなかすぐに思いつかなかったので記事として残してみることにしました。
自分への備忘録です。

記事にまとめてみると復習の意味でも結構いい勉強になりました。

問題となっているポイントについて

Swiftで書いているときにprotocolのメソッドで引数にデフォルト値を設定したくなる時があると思います。
例えば、こんな感じのコードですね。

Sample.swift
protocol CustomContentViewDelegate: AnyObject {
    func customContentViewDidTouchUp(_ view: CustomView, text: String? = nil) // コンパイルエラー
}

現状の仕様だとこのようにtext: String? =nilというのはprotocolでは持てない仕様である。
ですが仮にこれができた場合にはdelegateを呼び出し側で

Sample.swift
protocol CustomContentViewDelegate: AnyObject {
    func customContentViewDidTouchUp(_ view: CustomView, text: String? = nil)
}

class CustomView: UIView {
    weak var delegate: CustomContentViewDelegate?

    func sampleMethod() {
        // デフォルト引数発動
        delegate?.customContentViewDidTouchUp(self) 
        // 引数指定
        delegate?.customContentViewDidTouchUp(self, text: "テスト実装")
    }
}

という風にtextの引数を省略できて保守性が上がるというわけです。
この命題について考えたいと思います。

回答

いきなり回答に入りますがextensionでprotocolを拡張すれば良いわけです。

Sample.swift
protocol CustomContentViewDelegate: AnyObject {
    func customContentViewDidTouchUp(_ view: CustomView, text: String?)
}

// protocol extension でデフォルト引数を設定する
extension CustomContentViewDelegate {
    func customContentViewDidTouchUp(_ view: CustomView) {
        customContentViewDidTouchUp(view, text: nil)
    }
}

class CustomView: UIView {
    weak var delegate: CustomContentViewDelegate?

    func sampleMethod() {
        // デフォルト引数発動
        delegate?.customContentViewDidTouchUp(self) 
        // 引数指定
        delegate?.customContentViewDidTouchUp(self, text: "テスト実装") 
    }
}

これも一つのprotocol extensionの使い方かなと思い記事にしてみました。
ただ、コードレビューで指摘された時だと実際どんな風にしたら丁寧で見易いようになるかを2~3時間ぐらいかかりましたので
このような指摘をする場合には「protocol extensionで実装したらデフォルト引数ぽくなりますのでいいと思いますよ」と
指摘して頂ければすんなり解決した話しだったりします。

追記

個人の技術ブログをはじめました。
先月はこの個人ブログの設定やら記事の更新ならでQiitaへの投稿がおろそかになりました。

どうぞ宜しくおねがいします。

8
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4