3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数プロトコル指定つきオプショナル型の変数定義

Last updated at Posted at 2020-10-09

複数プロトコルを適用したOptional型の定義に戸惑ったのでメモ書きレベルです

複数プロトコルの適用した宣言

Optionalじゃないものはこれ。
安直に末尾に?をつけるとエラーになります.

.swift
class Hoge {
    var fuga: SomeClass & HogeProtocol & FugaProtocol 
}

複数プロトコルの適用したOptionalの宣言

書き方はいろいろありました。

カッコ

.swift
class Hoge {
    weak var delegate: (SomeClass & SomeProtocol & FugaProtocol)?
}

シンタックスシュガーなし

.swift
class Hoge {
    weak var delegate: Optional<SomeClass & SomeProtocol & FugaProtocol>
}

typealiasの利用。

.swift
typealias HogeDelegate = SomeClass & SomeProtocol & FugaProtocol
class Hoge {
    weak var delegate: HogeDelegate?
}
3
3
0

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?