16

Is there a way to list all Variables of a class in Swift?

For example:

class foo {
   var a:Int? = 1
   var b:String? = "John"
}

I want to list it like this: [a:1, b:"John"]

Hasan Akgün
  • 467
  • 1
  • 4
  • 15

4 Answers4

23

How you can do it in Swift 3.0 recursively:

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 {
                //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
Z. Bagley
  • 8,942
  • 1
  • 40
  • 52
Integer
  • 548
  • 4
  • 11
  • `label ` is nil somehow from the ObjC converted class (e.g. FirstClass is in ObjC). – allenlinli Jul 07 '17 at 09:03
  • This ObjC solution works instead https://stackoverflow.com/questions/13922581/is-there-a-way-to-log-all-the-property-values-of-an-objective-c-instance – allenlinli Jul 07 '17 at 09:13
8

The following should use reflection to generate the list of members and values. See fiddle at 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]
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • This answer http://stackoverflow.com/a/24069875/1187415 has a comment with a link to https://gist.github.com/mchambers/67640d9c3e2bcffbb1e2, where (if I understand it correctly) the actual values are retrieved and not only string descriptions. – Martin R Jan 13 '15 at 18:31
  • 1
    Did not know that. Thanks! Your `for` would be more "swifty" if written like `for index in 0.. – qwerty_so Jan 14 '15 at 15:52
3

Maybe a bit late for the party, but this solution using reflection and Mirror is 100% working:

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())
valvoline
  • 7,737
  • 3
  • 47
  • 52
0

I got clue from here. https://medium.com/@YogevSitton/use-auto-describing-objects-with-customstringconvertible-49528b55f446

This is a demo above Swift 4.0.

import Foundation

extension CustomStringConvertible {
    var description : String {
        var description: String = ""
        if self is AnyObject {  // unsafeAddressOf((self as! AnyObject))
            description = "***** \(type(of: self)) - <\(Unmanaged.passUnretained(self as AnyObject).toOpaque())>***** \n"
        } else {
            description = "***** \(type(of: self)) *****\n"
        }
        let selfMirror = Mirror(reflecting: self)
        for child in selfMirror.children {
            if let propertyName = child.label {
                description += "\(propertyName): \(child.value)\n"
            }
        }
        return description
    }
}

extension NSObject {
    var description: String {
        var description: String = ""
        if self is AnyObject {  // unsafeAddressOf((self as! AnyObject))
            description = "***** \(type(of: self)) - <\(Unmanaged.passUnretained(self as AnyObject).toOpaque())>***** \n"
        } else {
            description = "***** \(type(of: self)) *****\n"
        }
        let selfMirror = Mirror(reflecting: self)
        for child in selfMirror.children {
            if let propertyName = child.label {
                description += "\(propertyName): \(child.value)\n"
            }
        }
        return description
    }
}

class AA: CustomStringConvertible {
    var a: String = "aaa"
}

class BB: NSObject {
    var b: String = "bbb"
}

let  aa = AA()
print(aa)

let  bb = BB()
print(bb.description)

Output --

***** AA - <0x00000001001038e0>***** 
a: aaa

***** BB - <0x0000000100103310>***** 
b: bbb
Victor Choy
  • 4,006
  • 28
  • 35