簡體   English   中英

將Path String轉換為Swift中的Object Path

[英]Convert Path String to Object Path in Swift


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 : "abcd@xyz.com") 
let student = Student(rollNum : 1, name : "John", contact : contact)

此處,郵件 ID 的路徑以字符串“Student/contact/mailId”形式給出。 如何將其轉換為 Object 路徑作為 Student.contact.mailId?

假設 label 想要顯示郵件 ID,我會將路徑字符串作為“Student/contact/mailId”,而 label 應該將郵件 ID 顯示為 abcd@xyz.com

您的問題可以有很多解決方案。 我認為我的解決方案可以讓您了解如何解決這個問題。

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: "abcd@xyz.com")
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)))
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: "official@gmail.com", personal: "personal@gmail.com"))
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)
                
            }
        }
    }
}

在這里,我們可以獲得personal@gmail.com的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM