2017-09-29 83 views
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()也返回那些靜態屬性!

有沒有辦法呢?

回答

1

據我所知,答案是否定的。抱歉。即使在type(of: self)上獲得Mirror也不會有任何孩子。

相關問題