4
2

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 1 year has passed since last update.

【Swift】@discardableResult

Posted at

@discardableResult

@discardableResultとは返り値を気にしなくなるアノテーションです。
以下のように@discardableResultを付与した関数は値を返しているのも関わらず、警告が出ません。
必ずしも返り値を返す必要のない関数などを使用したいときには、このアノテーションを使い警告を消して利用しましょう。

@discardableResult
func double(num: Int) -> {
    return test * 2
}

func half(num: Int) -> {
    return test / 2
}

double(2) // 警告が出ない

half(4)   // 警告が出る
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?