/
Contents.swift
138 lines (100 loc) · 3.21 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//: Playground - noun: a place where people can play
import Foundation
var str = "Hello, playground"
enum TestEnum {
case HasPayload(Any)
case Doesnt
}
let t: TestEnum = .HasPayload("hi")
let optionalI = Optional<Int>(5)
let optionalIType = Optional<Int>.self
let x = reflect(t)
let oreflect = reflect(optionalI)
let www = oreflect.valueType
let isOptional = (oreflect.disposition == .Optional)
let c = oreflect.count
let optionalType = oreflect[0].1.valueType
let what = oreflect[0]
let one = what.0
let two : MirrorType = what.1
let twoType = what.1.valueType
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 generic<T : Reflectable>(value : T) {
let o = reflect(T.self)
explore(o, 4)
let mm = value.getMirror()
// let o = reflect(x)
explore(mm, 5)
let ot = reflect(value)
explore(ot, 6)
// let isOpt = o is Optional<Int>.self
}
let xxx : Int = 5
//generic(xxx)
generic(optionalI)