Is there a way to list all Variables of a class in Swift?
有没有办法在Swift中列出一个类的所有变量?
For example:
例如:
class foo {
var a:Int? = 1
var b:String? = "John"
}
I want to list it like this: [a:1, b:"John"]
我想这样列出:[a:1,b:“John”]
3 个解决方案
#1
7
The following should use reflection to generate the list of members and values. See fiddle at http://swiftstub.com/836291913/
以下内容应使用反射来生成成员和值列表。请参阅http://swiftstub.com/836291913/上的小提琴
class foo {
var a:Int? = 1
var b:String? = "John"
}
let obj = foo()
let reflected = reflect(obj)
var members = [String: String]()
for index in 0..<reflected.count {
members[reflected[index].0] = reflected[index].1.summary
}
println(members)
Output:
输出:
[b: John, a: 1]
#2
10
How you can do it in Swift 3.0 recursively:
你如何在Swift 3.0中递归地做到这一点:
import Foundation
class FirstClass {
var name = ""
var last_name = ""
var age = 0
var other = "abc"
func listPropertiesWithValues(reflect: Mirror? = nil) {
let mirror = reflect ?? Mirror(reflecting: self)
if mirror.superclassMirror != nil {
self.listPropertiesWithValues(reflect: mirror.superclassMirror)
}
for (index, attr) in mirror.children.enumerated() {
if let property_name = attr.label as String! {
//You can represent the results however you want here!!!
print("\(mirror.description) \(index): \(property_name) = \(attr.value)")
}
}
}
}
class SecondClass: FirstClass {
var yetAnother = "YetAnother"
}
var second = SecondClass()
second.name = "Name"
second.last_name = "Last Name"
second.age = 20
second.listPropertiesWithValues()
results:
结果:
Mirror for FirstClass 0: name = Name
Mirror for FirstClass 1: last_name = Last Name
Mirror for FirstClass 2: age = 20
Mirror for FirstClass 3: other = abc
Mirror for SecondClass 0: yetAnother = YetAnother
#3
0
Maybe a bit late for the party, but this solution using reflection and Mirror is 100% working:
也许派对有点晚了,但这个使用反射和镜像的解决方案100%正常工作:
class YourClass : NSObject {
var title:String
var url:String
...something other...
func properties() -> [[String: Any]] {
let mirror = Mirror(reflecting: self)
var retValue = [[String:Any]]()
for (_, attr) in mirror.children.enumerated() {
if let property_name = attr.label as String! {
retValue.append([property_name:attr.value])
}
}
return retValue
}
}
and somewhere in your code...
在你的代码中的某个地方......
var example = MoreRow(json: ["title":"aTitle","url":"anURL"])
print(example.listPropertiesWithValues())
#1
7
The following should use reflection to generate the list of members and values. See fiddle at http://swiftstub.com/836291913/
以下内容应使用反射来生成成员和值列表。请参阅http://swiftstub.com/836291913/上的小提琴
class foo {
var a:Int? = 1
var b:String? = "John"
}
let obj = foo()
let reflected = reflect(obj)
var members = [String: String]()
for index in 0..<reflected.count {
members[reflected[index].0] = reflected[index].1.summary
}
println(members)
Output:
输出:
[b: John, a: 1]
#2
10
How you can do it in Swift 3.0 recursively:
你如何在Swift 3.0中递归地做到这一点:
import Foundation
class FirstClass {
var name = ""
var last_name = ""
var age = 0
var other = "abc"
func listPropertiesWithValues(reflect: Mirror? = nil) {
let mirror = reflect ?? Mirror(reflecting: self)
if mirror.superclassMirror != nil {
self.listPropertiesWithValues(reflect: mirror.superclassMirror)
}
for (index, attr) in mirror.children.enumerated() {
if let property_name = attr.label as String! {
//You can represent the results however you want here!!!
print("\(mirror.description) \(index): \(property_name) = \(attr.value)")
}
}
}
}
class SecondClass: FirstClass {
var yetAnother = "YetAnother"
}
var second = SecondClass()
second.name = "Name"
second.last_name = "Last Name"
second.age = 20
second.listPropertiesWithValues()
results:
结果:
Mirror for FirstClass 0: name = Name
Mirror for FirstClass 1: last_name = Last Name
Mirror for FirstClass 2: age = 20
Mirror for FirstClass 3: other = abc
Mirror for SecondClass 0: yetAnother = YetAnother
#3
0
Maybe a bit late for the party, but this solution using reflection and Mirror is 100% working:
也许派对有点晚了,但这个使用反射和镜像的解决方案100%正常工作:
class YourClass : NSObject {
var title:String
var url:String
...something other...
func properties() -> [[String: Any]] {
let mirror = Mirror(reflecting: self)
var retValue = [[String:Any]]()
for (_, attr) in mirror.children.enumerated() {
if let property_name = attr.label as String! {
retValue.append([property_name:attr.value])
}
}
return retValue
}
}
and somewhere in your code...
在你的代码中的某个地方......
var example = MoreRow(json: ["title":"aTitle","url":"anURL"])
print(example.listPropertiesWithValues())