LoginSignup
7
7

More than 5 years have passed since last update.

Method Reference in Swift

Posted at

Java8の method reference 相当のものかなと思ったのですが、ちょっと違いそうです。クラスメソッドはともかく、インスタンスメソッドのほうは単なるカリー化なので使い所が難しいですね。

また、プロパティの参照には使えないので(算出プロパティはメソッドの糖衣構文であるにもかかわらず!)、やはり使い道はなさそう…。普通にクロージャを使いましょう。

// クラスメソッドの参照
let stringFromCString = String.fromCString
stringFromCString([102, 111, 111, 0]) // "foo"


// インスタンスメソッドの参照
let f = String.hasPrefix
f("foo bar")("foo") // "foo bar".hasPrefix("foo")とおなじ: true
f("foo bar")("bar") // "foo bar".hasPrefix("bar")とおなじ: false

(0 ..< 10).map(Int.advancedBy(10)) // [10, 11, 13, ... 19]
7
7
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
7
7