The following code has a fatal runtime error Could not extract a String from KeyPath Swift.KeyPath<Foo, Foundation.UUID>
.
import Foundation
final class Foo: NSObject, Protocol, Identifiable {
@objc
let id: UUID
override init() {
id = UUID()
super.init()
}
}
protocol Protocol {}
extension Protocol where Self: Identifiable {
static func fatal() {
let expression = NSExpression(forKeyPath: \Self.id)
print(expression)
}
}
// Works
let works = NSExpression(forKeyPath: \Foo.id)
print(works)
// Fatal error
Foo.fatal()
I feel like this is a bug since Foo
definitely makes id
available to the Objective-C code base (through @objc
). But perhaps I am just being too cute with the language for my own good.
Thoughts/help?