Skip to content

Instantly share code, notes, and snippets.

@creaaa
Created May 14, 2019 15:45
Show Gist options
  • Select an option

  • Save creaaa/eec1e5963c2ea97948a000d8c75e97a0 to your computer and use it in GitHub Desktop.

Select an option

Save creaaa/eec1e5963c2ea97948a000d8c75e97a0 to your computer and use it in GitHub Desktop.

Revisions

  1. creaaa created this gist May 14, 2019.
    20 changes: 20 additions & 0 deletions protocol + where
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import UIKit

    class HogeClass {}

    // 意味: このプロトコルに適合させるクラスは、
    // 必ずこのクラス(HogeClass)のサブクラスでなくてはならない
    // ≒ HogeClassを継承しないといけない
    // (Self == HogeClass とは書けない)
    protocol HogeProtocol where Self: HogeClass {
    func hoge()
    }
    //
    class FugaClass: HogeClass, HogeProtocol {
    func hoge() { print("hoge!") }
    }

    // つまりこれがだめ
    class PuyoClass: HogeProtocol {
    func hoge() { print("hoge!") }
    }