2
我有一個結構與協議一樣,在Swift中,是否有可能從結構中獲得所有**靜態**屬性?
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"
}
這裏Test().getAllProperties()
只返回bb
,它忽略了static
財產!
我想getAllProperties()
也返回那些靜態屬性!
有沒有辦法呢?