简体   繁体   中英

Is there any way to get the list of attributes of a class without any instantiated object?

I already know that we can get the list of attributes of a object using reflection in Swift, but all the examples that I found and have implemented so far uses a instantiate object. Something like that:

func attributeList() -> [String] {

    var attributeList = [String]()
    let serializableMirror = Mirror(reflecting: self) //using instantiate object

    for childMirror in serializableMirror.children {            
        if let label = childMirror.label {
            attributeList.append(label)
        }
    }
    return attributeList
}

My question is, there is any way to get the attributes of a class without any reference to it? Some kind of static method where I pass my desired class type and get the attributes list of it.

In all implementations of reflection that I've worked with you need an object for the reflection to work on.

Whether it's one you provide or one the system creates for you, you have to have a concrete object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM