I'm unable to solve this problem. I need a way to loop over a collection of key-path values but the compiler does not let me when the Value is an existentential or a protocol:
protocol A {}
protocol B {}
extension Int : A, B {}
extension String : A, B {}
struct Foo {
let x: Int = 42
let y: String = "swift"
}
let test: [KeyPath<Foo, A & B>] = [
\.x, // Key path value type 'Int' cannot be converted to contextual type 'A & B'
\.y
]
I also tried:
let test: [AnyKeyPath] = ....
let test2 = test.compactMap { $0 as? KeyPath<Foo, A & B> }
If you really need this, you can define computed properties on the type itself:
extension Foo {
fileprivate var x_AB: A & B { return self.x }
fileprivate var y_AB: A & B { return self.y }
}
let test: [KeyPath<Foo, A & B>] = [\.x_AB, \.y_AB]
Or you can use closures instead of key paths:
let test: [(Foo) -> A & B] = [
{ $0.x },
{ $0.y }
]
(note: I didn't test either of these code samples, so they may have small bugs)
Weird. Does cameraSetting by chance come from a superclass of BluetoothCharacteristic? @rudkx fixed a bug related to that recently. If not, this is definitely worth a bug of its own.