I have a simple class and I want to use keypath in the init
, something like this:
class V: UIView {
convenience init() {
self.init(frame: .zero)
self[keyPath: \.alpha] = 0.5
}
}
let v = View()
When I run this code I get a runtime error:
Fatal error: could not demangle keypath type from ' ����XD':
But, if I specify the type in keyPath it works fine:
class V: UIView {
convenience init() {
self.init(frame: .zero)
self[keyPath: \UIView.alpha] = 0.5
}
}
let v = View()
print(v.alpha) \\ prints 0.5
But, what's even stranger is that this code works:
class V: UIView {
convenience init() {
self.init(frame: .zero)
foo()
}
func foo() {
self[keyPath: \.alpha] = 0.5
}
}
let v = View()
print(v.alpha) \\ prints 0.5
What is the actual reason for this error?