Skip to content

kostiakoval/Mirror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mirror

CI Status Version License Platform

Swift objects Reflection API - 100%, no Objc runtime

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

###Create a mirror for an instance

struct Person {
  let name: String
  var age: Int
}

var person = Person(name: "Jon", age: 27)
let mirror = Mirror(person)

Inspect it

Type properties names

mirror.names  
//["name", "age"]

Type properties values

mirror.values
//["Jon", 27]

Type properties types

mirror.types
//[Swift.String, Swift.Int]

Type properties types short notation, skip bundle

mirror.typesShortName
//["String", "Int"]

Get property values by name

mirror["name"] // "Jon"
mirror["age"]  // 27

Convert to dictionary with [PropertyName : PropertyValues]

mirror.toDictionary
//["age": 27, "name": "Jon"]

Mirror implements CollectionType, SequenceType.

Iterate over it's children MirrorItems

for item in mirror {
  println(item)
}
//name: Swift.String = Jon
//age: Swift.Int = 27

Get children MirrorItems

let children = mirror.children //Array of MirrorItem
let firstKid = children[0]
//{name: "name", type: Swift.String, value: "Jon" }

Requirements

  • Swift 1.2

Installation

Mirror is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Mirror"

Author

Kostiantyn Koval, konstantin.koval1@gmail.com

License

Mirror is available under the MIT license. See the LICENSE file for more info.