Skip to content

Instantly share code, notes, and snippets.

@jiverson
Forked from mchambers/reflect.swift
Last active August 29, 2015 14:14
Show Gist options
  • Save jiverson/2ba3831bacfba3289ce4 to your computer and use it in GitHub Desktop.
Save jiverson/2ba3831bacfba3289ce4 to your computer and use it in GitHub Desktop.
// Let's define a basic Swift class.
class Fruit {
var type=1
var name="Apple"
var delicious=true
}
// We can get at some info about an instance of an object using reflect(), which returns a Mirror.
reflect(Fruit()).count
reflect(Fruit())[1].0
reflect(Fruit())[1].1.summary
// Dump a bunch of info about the object using reflection.
dump(Fruit())
// Let's make an instance and print all its properties to the console.
var theFruit=Fruit()
for var index=0; index<reflect(theFruit).count; ++index {
println(reflect(theFruit)[index].0 + ": "+reflect(theFruit)[index].1.summary)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment