5
6

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 5 years have passed since last update.

optional funcとenumは共存できない

Posted at

protocolにoptionalな関数を加えたい場合、@objcを書き加える必要がある。

@objc protocol Hoge {
	optional func hogehoge()
}

@objcがない場合、 'optional' can only be applied to members of an @objc protocol といったコンパイルエラーが発生する。

しかし、@objcをつけたプロトコル内部では、クラス型のみが記述できる。
したがって、structやenumは@objcプロトコル内に記述できない。

enum Piyo {
}

@objc protocol Hoge {
	// コンパイルエラー "Method cannnot be marked @objc because the type of the parameter cannnot be represented in Objective-C"
    optional func hogehoge(piyo: Piyo)
}

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?