Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI Insert a field name received from a function following "."

I get a field name from a function. How to insert that field name following dot "."

  1. Model
struct User: Identifiable {
        let username: String
        let fullName: String
        var description: String = ""
    }
  1. Get a field name from a title
func get_field_name(key: String?) -> String {
    var default_field = ""
    guard let key = key else { return default_field }
    let field: [String: String] = [
        "Name" : "fullName",
        "Username" : "username",
        "Bio" : "description"
    ]
    return field[key] ?? default_field
}
  1. Expect.

Example if "item.title" is "Name" then

let user: User
Text(item.title) //OK - Name
Text(user.get_field_name(key: item.title)) //OK - fullName
Text(user.???) //??? How to insert field name following "."
  1. Purpose

I use it in the pic below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Thank you so much for your answer.

>Solution :

I think you are looking for key paths.

Change get_field_name to:

func getUserKeyPath(key: String?) -> KeyPath<User, String>? {
    guard let key = key else { return nil }
    let field: [_: KeyPath<User, _>] = [
        "Name" : \.fullName,
        "Username" : \.username,
        "Bio" : \.description
    ]
    return field[key]
}

Then you can do:

if let keyPath = getFieldKeyPath(key: item.title) {
    Text(user[keyPath: keyPath])
} else {
    // handle the case when item.title does not match any property name in User
}

You can also do this in a single expression if you want to show a default string in the Text when item.title does not match any property name:

Text(getFieldKeyPath(key: "Name").map { user[keyPath: $0] } ?? "Unknown")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading