我有一个像<,p>这样的协议的结构
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()
也返回那些静态属性!
有什么办法吗?
答案 0 :(得分:3)
据我所知,答案是否定的。抱歉。即使在Mirror
上获得type(of: self)
,也不会有任何子女。