Skip to content

Instantly share code, notes, and snippets.

@erica
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/c058d1d10fd0f1f04c91 to your computer and use it in GitHub Desktop.
Save erica/c058d1d10fd0f1f04c91 to your computer and use it in GitHub Desktop.
Reflection work
import Foundation
func QLString(x : QuickLookObject) -> String {
switch x {
case .Text(let s) : return "[Text] " + s
case .Int(let i) : return "[Int] " + "\(i)"
case .UInt(let u) : return "[UInt] " + "\(u)"
case .Float(let f) : return "[Float] " + "\(f)"
case .Image(let img) : return "[Image] " + "[image]"
case .Sound(let snd) : return "[Sound] " + "[sound]"
case .Color(let color) : return "[Color] " + "\(color)"
case .BezierPath(let path) : return "[Bezier] " + "[bezier]"
case .AttributedString(let s) : return "[Attributed String] " + "\(s)"
case .Rectangle(let x, let y, let w, let h) : return "[Rect] " + "[\(x), \(y), \(w), \(h)]"
case .Point(let x, let y) : return "[Point] " + "(\(x), \(y))"
case .Size(let w, let h) : return "[Size] " + "\(w), \(h)"
case .Logical(let v) : return "[Bool] " + (v ? "true" : "false")
case .Range(let a, let b) : return "[Range] " + "\(a)..<\(b)"
case .View(let v) : return "[View] " + "\(v)"
case .Sprite(let s) : return "[Sprite] " + "\(s)"
case .URL(let u) : return "[URL] " + "\(u)"
default: return "<Unknown>"
}
}
func DispositionString(disposition : MirrorDisposition) -> String {
switch disposition {
case .Aggregate: return "Aggregate"
case .Class: return "Class"
case .Container: return "Container"
case .Enum: return "Enum"
case .IndexContainer : return "Index Container (Array)"
case .KeyContainer : return "Key Container (Dict)"
case .MembershipContainer : return "Membership Container"
case .Optional : return "Optional"
case .Struct: return "Struct"
case .Tuple: return "Tuple"
case .ObjCObject: return "ObjC Object"
}
}
func DispositionString(item : Any) -> String {
return DispositionString(reflect(item).disposition)
}
func typestring(x : Any) -> String {
if "\(x)" == "(Metatype)" {
return "<Metatype>"
}
return _stdlib_getDemangledTypeName(x)
}
func TupleDisposition(mirror : MirrorType) -> String {
if (mirror.disposition != .Tuple) {return ""}
var array = [String]()
for reference in 0..<mirror.count {
let (name, referenceMirror) = mirror[reference]
array += [typestring(referenceMirror.value)]
}
return array.reduce(""){"\($0),\($1)"}
// return NSArray(array:array).componentsJoinedByString(", ") as String
}
func ExploreItem(mirror : MirrorType, _ indent:Int = 0) {
var indentString = ""; indentString.extend(Repeat(count: indent * 2, repeatedValue:" " as Character))
let rs = ""
var ts = typestring(mirror.value)
if (mirror.disposition == .Tuple) {
ts = TupleDisposition(mirror)
}
// func dump<T>(x: T, name: String? = default, indent: Int = default,
// maxDepth: Int = default, maxItems: Int = default) -> T
println("\(indentString)Dump: ")
dump(mirror.value, indent:2 * indent, maxDepth:1)
if let qlobject = mirror.quickLookObject {
let qlstring = QLString(qlobject)
println("\(indentString)QuickLook: \(qlstring)\(rs)")
}
println("\(indentString)Disposition: \(DispositionString(mirror.disposition)) [\(ts)]\(rs)")
if let identifier = mirror.objectIdentifier {
println("\(indentString)Identifier: \(mirror.objectIdentifier)\(rs)")
}
println("\(indentString)ValueType: \(mirror.valueType)\(rs)")
println("\(indentString)Value: \(mirror.value)\(rs)")
println("\(indentString)Summary: \(mirror.summary)\(rs)")
if (mirror.count == 0) {return}
println("\(indentString)Subreferences: \(mirror.count) children\(rs)")
for reference in 0..<mirror.count {
let (name, subreference) = mirror[reference]
println("\(indentString)Element Name: \(name)\(rs)")
ExploreItem(subreference, indent + 4)
}
}
func ExploreItem<T>(item : T) {
println("---------------------")
println("Exploring \(item)")
println("---------------------")
ExploreItem(reflect(item), 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment