ӳจͷΑ͏ͳߏจʹ / ҾΛίϯύΫτʹ Ͱ͖Δ
class Label {
var text = ""
var fontSize = 12
}
class ProductModel {
var name = "kalupas"
}
let model = ProductModel()
let label = Label()
Slide 14
Slide 14 text
Label ʹ ProductModel ͷ property Λ bind ͢Δ
class Label {
var text = ""
var fontSize = 12
}
class ProductModel {
var name = "kalupas"
}
let model = ProductModel()
let label = Label()
Slide 15
Slide 15 text
Label ʹ ProductModel ͷ property Λ bind ͢Δ
class Label {
var text = ""
var fontSize = 12
}
class ProductModel {
var name = "kalupas"
}
let model = ProductModel()
let label = Label()
bind(model, \.name, to: label, \.text)
Slide 16
Slide 16 text
Label ʹ ProductModel ͷ property Λ bind ͢Δ
class Label {
var text = ""
var fontSize = 12
}
class ProductModel {
var name = "kalupas"
}
let model = ProductModel()
let label = Label()
bind(model, \.name, to: label, \.text)
• ӳจͷΑ͏ʹಡΊΔ
• Bind model name to label text
• getter, setter Λ؆ܿʹهड़Ͱ͖Δ
• \.name = KeyPath (getter)
• \.text = ReferenceWritableKeyPath (getterɾsetter)
Case Paths ͷجຊ
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
Slide 26
Slide 26 text
Case Paths ͷجຊ
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
/Authentication.authenticated // CasePath
ʮ/Enum.Caseʯͱ͍͏ߏจͰ
CasePath ΛखʹೖΕΒΕΔ
Slide 27
Slide 27 text
enum ʹ associated value ΛೖΕ͍ͨ࣌ (௨ৗ)
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = Authentication.authenticated(
accessToken: "cafebeef"
)
Slide 28
Slide 28 text
enum ʹ associated value ΛೖΕ͍ͨ࣌ (௨ৗ)
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = Authentication.authenticated(
accessToken: "cafebeef"
)
Slide 29
Slide 29 text
enum ͷ associated value ΛऔΓग़͍ͨ࣌͠ (௨ৗ)
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = Authentication.authenticated(
accessToken: "cafebeef"
)
if case let .authenticated(accessToken) = authentication {
// use accessToken
}
Slide 30
Slide 30 text
enum ͷ associated value ΛऔΓग़͍ͨ࣌͠ (௨ৗ)
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = Authentication.authenticated(
accessToken: "cafebeef"
)
if case let .authenticated(accessToken) = authentication {
// use accessToken
}
Slide 31
Slide 31 text
Case Paths Λͬͯ associated value ΛೖΕΔ
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = Authentication.authenticated(
accessToken: "cafebeef"
)
Slide 32
Slide 32 text
Case Paths Λͬͯ associated value ΛೖΕΔ
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
// let authentication = Authentication.authenticated(
// accessToken: "cafebeef"
// )
let authentication = (/Authentication.authenticated).embed("cafebeef")
Slide 33
Slide 33 text
Case Paths Ͱ associated value ΛऔΓग़͢
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = (/Authentication.authenticated).embed(“cafebeef")
if case let .authenticated(accessToken) = authentication {
// use accessToken
}
Slide 34
Slide 34 text
Case Paths Ͱ associated value ΛऔΓग़͢
enum Authentication {
case authenticated(accessToken: String)
case unauthenticated
}
let authentication = (/Authentication.authenticated).embed(“cafebeef")
// if case let .authenticated(accessToken) = authentication {
// // use accessToken
// }
(/Authentication.authenticated).extract(from: authentication)
Case Paths ͷ͓͔͛Ͱ enum ͷಛఆͷ case ʹ
Ԡͨ͡ॲཧΛهड़Ͱ͖ΔΑ͏ʹͳΔ
extension View {
func sheet(
unwrapping enum: Binding,
case casePath: CasePath,
@ViewBuilder content: @escaping (Binding) -> Content
) -> some View where Content: View {
self.sheet(
isPresented: `enum`.case(casePath).isPresented()
) {
Binding(
unwrapping: `enum`.case(casePath)
).map(content)
}
}
}
Enum ͷಛఆͷ case ͕
active ͳ࣌ʹͷΈ
දࣔ͞ΕΔ sheet
Slide 46
Slide 46 text
Case Paths ͷ࣮໘നϙΠϯτ
• pre
fi
x, in
fi
x operator
• @_disfavoredOverload
• Mirror
• Pointer
46
Slide 47
Slide 47 text
Case Paths ͷ࣮໘നϙΠϯτ
• pre
fi
x, in
fi
x operator
• @_disfavoredOverload
• Mirror
• Pointer
• ࠓͷൃද·Ͱʹઆ໌Ͱ͖ΔϨϕϧ·Ͱམͱ͠ࠐΊͳ͔ͬͨͷͰɺ
·ͨͷػձʹهࣄʹ͢Δ͔ൃද͠Α͏ͱࢥ͍·͢🙇
47
Slide 48
Slide 48 text
Case Paths ͷ࣮໘നϙΠϯτ
• pre
fi
x, in
fi
x operator
• @_disfavoredOverload
• Mirror
• Pointer
48
Slide 49
Slide 49 text
pre
fi
x, in
fi
x operator
• pre
fi
x: ಄ࣙ
• in
fi
x: தࣙ
• post
fi
x (ඌࣙ) ͋Δ͕ɺͦΕར༻͞Ε͍ͯͳ͍ͷͰׂѪ
• pre
fi
x operator ͷྫ
• Key Paths ʹ͓͚Δʮ\User.idʯɺBoolean ͷసʮ!isPresentedʯͳͲ
• in
fi
x operator ͷྫ
• ʮ1 + 2ʯʮ2 - 1ʯͳͲ
• operator ࣗ࡞͢Δ͜ͱ͕Ͱ͖Δ
49
Slide 50
Slide 50 text
Case Paths ʹ͓͚Δ pre
fi
x operatorʮ/ʯͷࣗ࡞
prefix operator /
public prefix func / (
embed: @escaping (Value) -> Root
) -> CasePath {
.init(embed: embed, extract: extractHelp(embed))
}
Slide 51
Slide 51 text
Case Paths ʹ͓͚Δ pre
fi
x operatorʮ/ʯͷࣗ࡞
prefix operator /
public prefix func / (
embed: @escaping (Value) -> Root
) -> CasePath {
.init(embed: embed, extract: extractHelp(embed))
}
Slide 52
Slide 52 text
Case Paths ʹ͓͚Δ pre
fi
x operatorʮ/ʯͷࣗ࡞
prefix operator /
public prefix func / (
embed: @escaping (Value) -> Root
) -> CasePath {
.init(embed: embed, extract: extractHelp(embed))
}
Slide 53
Slide 53 text
Case Paths ʹ͓͚Δ pre
fi
x operatorʮ/ʯͷࣗ࡞
prefix operator /
public prefix func / (
embed: @escaping (Value) -> Root
) -> CasePath {
.init(embed: embed, extract: extractHelp(embed))
}
/Authentication.authenticated
·ͱΊ
• struct ʹ͓͚Δ Key Paths ͷΑ͏ͳػೳΛ enum Ͱ࣮ݱ͢Δ Case Paths ͱ͍͏ϥΠϒϥϦ͕ Point-Free ʹΑͬͯ
ఏڙ͞Ε͍ͯΔ
• Case Paths ʹ༷ʑͳར༻γʔϯ͕͋Δ
• map ͳͲͷߴ֊ؔͰͷར༻ɺfunction ͷҾͱͯ͠ಛఆͷ case Λ׆༻͍ͨ͠߹ͳͲ
• Case Paths ͷ࣮ͰҎԼͷΑ͏ͳٕज़͕ར༻͞Ε͍ͯΔ
• pre
fi
x, in
fi
x operator
• @_disfavoredOverload
• Mirror (ࠓར༻͞Ε͍ͯͳ͍)
• Pointer
• Case Paths ͔ͬ͠Γςετ͞Ε͍ͯͯར༻࣮͋ΓɺݴޠػೳʹΈࠐΉͷͨΊͷಈ͖ൃੜ͍ͯ͠Δ