I know the below is possible in Javascript. Is there anything similar I can use in SwiftUI to pass an object property as a String?
var a = { foo: 123 };
a.foo // 123
a['foo'] // 123
var str = 'foo';
a[str] // 123
What you likely want here is a key path. For example, given:
struct A {
var foo: Int
}
You can construct an A, and access it:
let a = A(foo: 123)
a.foo // 123
And given that, you can access the foo property by key path:
let kp = \A.foo
a[keyPath: kp] // 123
If your actual goal is to map strings to integer, that's just a [String: Int], and it would work identically. If you mean "I want to pass strings to objects has have fairly random things happen, and possibly crash like they do in JavaScript," then that's also possible, using a custom subscript like subscript(key: String) -> Int?.
var a = { foo: 123 };anda.fooare like a class/struct.a['foo']anda[str]look like dictionaries.