22
23

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.

iOS/swiftでselectorを使った際に引数なしのメソッドを指定しているにもかかわらず":"をつけていて時間を取られたので備忘録

Posted at
  • selectorで指定するメソッドが引数をとる場合
  • コロンが必要
override func viewDidLoad() {
	// ...
	UIRefreshControl().addTarget(
		self, 
		action: "引っ張って更新:", //←ここ
		forControlEvents: UIControlEvents.ValueChanged
	)
	// ...
}
func 引っ張って更新(_:AnyObject){
	// ...
}
  • selectorで指定するメソッドが引数を取らない場合
  • コロンが不要
  • コロンをつけると下記出力が出る
  • *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[hoge.ViewController 引っ張って更新:]: unrecognized selector sent to instance 0x145e187d0'

override func viewDidLoad() {
	// ...
	UIRefreshControl().addTarget(
		self, 
		action: "引っ張って更新", //←ここ
		forControlEvents: UIControlEvents.ValueChanged
	)
	// ...
}
func 引っ張って更新(){
	// ...
}

selectorに初めて触れ、ググっていると「コロンつけるべし」という記述が多かったのでつけていたらそれらは引数ありメソッドを指定していたという。きちんとコロンの意味まで調べないとだめですね。

22
23
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
22
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?