-
-
Save natecook1000/4ee3ee560000062b1ace to your computer and use it in GitHub Desktop.
Swift: Reflection dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original by Erica Sadun | |
// Source: http://ericasadun.com/2014/06/24/swift-reflection-dump/ | |
import UIKit | |
import Foundation | |
func typestring(x : Any) -> String | |
{ | |
if let obj = x as? NSObject { | |
return NSStringFromClass((x as NSObject).dynamicType) | |
} | |
// Native Swift | |
switch x { | |
case let test as Double: return "Double" | |
case let test as Int: return "Int" | |
case let test as Bool: return "Bool" | |
case let test as String: return "String" | |
default: break | |
} | |
switch x { | |
case let test as [Double]: return "[Double]" | |
case let test as [Int]: return "[Int]" | |
case let test as [Bool]: return "[Bool]" | |
case let test as [String]: return "[String]" | |
default: break | |
} | |
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 .ObjCObject : return "ObjC Object" | |
case .Optional : return "Optional" | |
case .Struct: return "Struct" | |
case .Tuple: return "Tuple" | |
} | |
} | |
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)"} | |
} | |
func explore(mirror : MirrorType, _ indent:Int = 0) | |
{ | |
// dump(mirror.value) // useful | |
let indentString = String(count: indent, repeatedValue: " " as Character) | |
var ts = typestring(mirror.value) | |
if (mirror.disposition == .Tuple) { | |
ts = tupleDisposition(mirror) | |
} | |
println("\(indentString)Disposition: \(dispositionString(mirror.disposition)) [\(ts)]") | |
println("\(indentString)Identifier: \(mirror.objectIdentifier)") | |
println("\(indentString)ValueType: \(mirror.valueType)") | |
println("\(indentString)Value: \(mirror.value)") | |
println("\(indentString)Summary: \(mirror.summary)") | |
for reference in 0..<mirror.count { | |
let (name, subreference) = mirror[reference] | |
println("\(indentString)Element Name: \(name)") | |
explore(subreference, indent + 4) | |
} | |
} | |
func explore<T>(item : T) | |
{ | |
println("---------------------") | |
println("Exploring \(item)") | |
println("---------------------") | |
explore(reflect(item)) | |
println() | |
} | |
explore("Hello") | |
explore((3, 4)) | |
explore(NSURL(string:"http://ericasadun.com")) | |
explore(["A", "B"]) | |
explore([3:2,6:4,9:7]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment