struct Person: Codable {
var name: String
var age: Int
var email: String?
}
func getProperties(of codable: Codable) -> [String] {
let mirror = Mirror(reflecting: codable)
return mirror.children.compactMap { $0.label }
}
let person = Person(name: "John Doe", age: 30, email: "john@example.com")
let propertiesList = getProperties(of: person)
print(propertiesList)
网友评论