Reputation: 36158
I have a method that takes a key path for a type, however it is giving me a compile error when I try to use the shorthand version of the key path.
Here's what I'm trying to do:
struct MyType {
let prop1: String
let prop2: Int
let prop3: Bool
}
func test(_ keyPath: PartialKeyPath<MyType>) {
switch keyPath {
case \MyType.prop1:
print("prop1")
case \MyType.prop2:
print("prop1")
case \MyType.prop3:
print("prop1")
default:
break
}
}
test(\MyType.prop1)
In the switch
statement, is it possible to do case \.prop1
instead of specifying the root? When I try this I get this error:
Type of expression is ambiguous without more context
It is also forcing me to specify a default
case even though I've specified all the properties of the type. Is there a reason for this as well? Thanks for any help.
Upvotes: 0
Views: 230