【iOS,Swift3】Extension晒す〜番外編〜Propertiesの巻
表題の通りです。ご査収ください。
import Foundation public protocol Properties { func properties() -> [String: Any?] } extension Properties { public func properties() -> [String: Any?] { var dic = [String: Any?]() Mirror(reflecting: self).superclassMirror?.children.forEach { (child) in if let key = child.label { dic[key] = child.value } } Mirror(reflecting: self).children.forEach { (child) in if let key = child.label { dic[key] = child.value } } return dic } }
補足。こんな感じで使います。
internal class Hoge: Properties { internal var printProperties { properties().forEach { (key, value) in print("PropertyName: \(key) \nValue:\(String(describing: value))") } } }