Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, is it possible to get all the **static** properties from a struct?

Tags:

I have a struct with a protocol like,

protocol Page {
    func getAllProperties () -> [String: Any] 
}

extension Page {
    public func getAllProperties () -> [String: Any] {
        var result: [String: Any] = [:]
        let mirror = Mirror(reflecting: self)
        print(mirror)
        for (labelMaybe, valueMaybe) in mirror.children {
            print(labelMaybe)
            guard let label = labelMaybe else {
                continue
            }

            result[label] = valueMaybe
        }

        return result
    }

}


struct Test: Page {
    static let aa = "aaaaa"
    let bb = "bbbb"
}

Here Test().getAllProperties() returns only bb, it omits the static property!!!

I want that getAllProperties() return those static property too!

is there any way for it?

like image 387
Raja Avatar asked Sep 29 '17 23:09

Raja


1 Answers

To the best of my knowledge, the answer is no. Sorry. Even getting a Mirror on the type(of: self) doesn't end up having any children.

like image 166
Lily Ballard Avatar answered Sep 23 '22 17:09

Lily Ballard