0

struct Student{
  var rollNum : Int
  var name : String
  var contact : Contact
}
struct Contact{
  var phoneNum : String
  var mailId : String
}

let contact = Contact(phoneNum : "1234567890", mailId : "[email protected]") 
let student = Student(rollNum : 1, name : "John", contact : contact)

Here, the path for mail Id is given as a String "Student/contact/mailId". How to convert this to Object Path as Student.contact.mailId?

Assume the label wants to display the mail ID, I would give the path string as "Student/contact/mailId" and the label should display the mail id as [email protected]

5
  • What exactly is an "Object Path"? Do you mean a KeyPath or something else? Commented Feb 22, 2022 at 9:57
  • ` let contact = Contact(phoneNum : "1234567890", mailId : "[email protected]") let student = Student(rollNum : 1, name : "John", contact : contact) ` Consider a label and it's text is given by this object path (i.e) Assume the label wants to display the mail ID, I would give the path string as "Student/contact/mailId" and the label should display the mail id as [email protected] @joakim-danielson Commented Feb 22, 2022 at 10:39
  • You should add that info to the question. Commented Feb 22, 2022 at 10:43
  • @JoakimDanielson Thanks!! Added further info...Kindly let me know if u get the solution Commented Feb 22, 2022 at 13:25
  • What are you trying to achieve here? Why would you access the mailId via ' path ' instead of accessing via object chain? Commented Feb 22, 2022 at 13:28

2 Answers 2

1

There can be many solutions to your problem. I think my solution could give an idea of how can you solve this problem.

struct Student {
    var rollNum: Int
    var name: String
    var contact: Contact
}

struct Contact {
    var phoneNum: String
    var mailId: String
}

func studentInformation(student: Student, paths: [String]) {
    print("Student name: \(student.name), RollNumber: \(student.rollNum)")
    if paths[0] == "\(Contact.self)" {
        contactInformation(contact: student.contact, path: paths[1])
    }
}

func contactInformation(contact: Contact, path: String) {
    print("Contact phonenumber: \(contact.phoneNum), mailId: \(contact.mailId)")
    let contactVariableNameWithValue = Mirror(reflecting: contact).children
    for variableName in contactVariableNameWithValue {
        if variableName.label == path {
            print(variableName.value)
            break
        }
    }
}

let contact = Contact(phoneNum: "1234567890", mailId: "[email protected]")
let student = Student(rollNum: 1, name: "John", contact: contact)
var pathString = "Student/Contact/mailId"
public let pathComponents = pathString.components(separatedBy: "/")
studentInformation(student: student, paths: Array(pathComponents.suffix(from: 1)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @md-abul-kashem , It greatly helped me to solve my problem
@LakshmiC Follow How to say thanks in SO
0
struct Student {
    var rollNum: String
    var name: String
    var contact: Contact
}
struct Contact {
    var phoneNum: String
    var mailId: MailId
}
struct MailId {
    var official : String
    var personal : String
}
let pathString = "Student/contact/mailId/personal"
let pathComponents = pathString.components(separatedBy: "/")
var index : Int = 1

let contact = Contact(phoneNum: "9988998899", mailId: MailId(official: "[email protected]", personal: "[email protected]"))
let student = Student(rollNum: "123", name: "John", contact: contact)



Reflection(from: student , pathComponents: pathComponents)
func Reflection(from object : Any, pathComponents : [String]) {
    let properties = Mirror(reflecting: object)
    for property in properties.children{
        if index < pathComponents.count{
            if property.label == pathComponents[index] {
                index += 1
                print(property.value , "got here with label :" , property.label)
                Reflection(from: property.value, pathComponents: pathComponents)
                
            }
        }
    }
}

Here, we can get the value as [email protected]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.