[Swift] KeyPathとWritableKeyPathの変換
DRANK

KeyPathは生成する場所によって取りうる型が変わります。struct Hoge{ let a: Int = 0 var b: Int = 1 private(set) var c: Int = 2 private var d: Int = 3 func test(){ let writableKeyPath_a: WritableKeyPath<Hoge, Int> = \.a let keyPath_a: KeyPath<Hoge, Int> = \.a let writableKeyPath_b: WritableKeyPath<Hoge, Int> = \.b let writableKeyPath_c: WritableKeyPath<Hoge, Int> = \.c let writableKeyPath_d: WritableKeyPath<Hoge, Int> = \.d } } func test(){ let writableKeyPath_a: WritableKeyPath<Hoge, Int> = \.a let keyPath_a: KeyPath<Hoge, Int> = \.a let writableKeyPath_b: WritableKeyPath<Hoge, Int> = \.b let writableKeyPath_c: WritableKeyPath<Hoge, Int> = \.c let keyPath_c: KeyPath<Hoge, Int> = \.c let writableKeyPath_d: WritableKeyPath<Hoge, Int> = \.d } さて、格納する全ての値の変更の後に必ず所定の処理を実…

zenn.dev
Related Topics:
1 comments
  • 使い道がありそうでなさそうなKeyPathについての記事です。