Skip to content

Instantly share code, notes, and snippets.

@LazyClutch
Last active September 24, 2018 19:59
Show Gist options
  • Save LazyClutch/16a3096538cd938c5b74 to your computer and use it in GitHub Desktop.
Save LazyClutch/16a3096538cd938c5b74 to your computer and use it in GitHub Desktop.
Swift Reflection
// The test code of Swift Reflection API: http://appventure.me/2015/10/24/swift-reflection-api-what-you-can-do/
import UIKit
public class Store {
let storesToDisk: Bool = true
}
public class BookmarkStore: Store {
let itemCount: Int = 10
}
public struct Bookmark {
enum Group {
case Tech
case News
}
private let store = {
return BookmarkStore()
}()
let title: String?
let url: NSURL
let keywords: [String]
let group: Group
}
let aBookmark = Bookmark(title: "Appventure", url: NSURL(string: "appventure.me")!, keywords: ["Swift", "iOS", "OSX"], group: .Tech)
let aMirror = Mirror(reflecting: aBookmark)
print(aMirror.displayStyle)
print(aMirror.subjectType)
for case let (label?, value) in aMirror.children {
print (label, value)
}
let closure = { (a: Int) -> Int in return a * 2 }
let bMirror = Mirror(reflecting: closure)
print(bMirror.displayStyle)
print(aMirror.subjectType)
print(Mirror(reflecting: 5).subjectType)
print(Mirror(reflecting: "test").subjectType)
print(Mirror(reflecting: NSNull()).subjectType)
let cMirror = Mirror(reflecting: aMirror)
print(cMirror.displayStyle)
print(cMirror.subjectType)
for case let (label?, value) in cMirror.children {
print (label, value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment