Giter VIP home page Giter VIP logo

mirror's Introduction

Mirror

CI Status Version Carthage compatible License Platform

Mirror - Swift objects Reflection API. It's 100% Swift, no Objc runtime.
It's possible to do reflection of Swift object. And it doesn’t need to be hard.

##Features

  • 100% Pure Swift implementation
  • Easy to use
  • Powerful
  • Even more Powerful (Coming soon)

Usage

If you want to try it, there is a demo Playground available, just run

pod try Mirror

###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

//Get information about the type of an instance

mirror.name
//"MirrorTest.Person"

mirror.shortName
//Person

mirror.memorySize
// 32

mirror.isClass
//false

mirror.isStruct
//true

Type Properties Inspection

Get information about content of the type, its properties

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

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

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

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

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

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

Mirror is a CollectionType

All the CollectionType methods are available for use with mirror
Iterating, count, map, filter and other

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

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

var mirP = mirror[1]
mirP.name   // "age"
mirP.value  // 27
mirP.type   // Swift.Int

Installation

###CocoaPods

To install it, simply add the following line to your Podfile:

use_frameworks!
pod "Mirror"

###Carthage

To integrate Mirror into your Xcode project using Carthage, specify it in your Cartfile:

github "kostiakoval/Mirror"

Contribute

Please open an issue with bugs and missing features, functionality or ideas for improvements.
Also you can contribute by following this guidelines:

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create pull request

Author

Kostiantyn Koval, [email protected]

License

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

mirror's People

Contributors

kostiakoval avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mirror's Issues

A mirror child items should be also a Mirror items

When I get a Mirror child I want to be able to use all the same methods as on original mirror
Example:

struct A {
 let a = 10
}

struct B {
 let x = [1, 2, 3]
 let a:  A()

let m = Mirror(B())
let ar = m.children[0]
ar.isArray
... etc 

Create new instance from Mirror

Something like this would be great:

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

var person = Person(name: "Jon", age: 27)
var mirror = Mirror(person)
// make some changes to mirror
var person2 = Person(mirror: mirror) // a modified version of Jon

Expect more features

You have realized the following functions,thanks:
mirror.isClass
mirror.isStruct

But we may need more good features, such as:
mirror.isArray
mirror.isOptional

can you support Accurate property’s type?

for example,i have a Class,

Class Person{
  var name: String
  var age: Int
  var friend: [Person]
}

you had support the properties’s normal types
but, if properties’s type is [CustomClass], or [CustomClass]? ,your type is not Accurate

can you add a isArray for properties?

english is pool, sorry,,,,,,,thanks!!!!!!!

Element type of empty array?

With your mirror framework, is it possible to get the Element type of an empty array?

let a:Array = []

And then get String somehow?

Mirror a Class ,not a instance

you see,

a Class ,has it’s properties,but,to get it’s properties,i need init a instance.

can you support Mirror a Class directly?

like this:

let mirror = Mirror(Student)

thank you!!!

typesShortName is sth wrong

if my class has any optional properties,your typesShortName is not work well:

such as:

class Person{
    var name: String?
    var age: Int?
    var dog: Dog?
    var dogs: [Dog]?
}

class Dog {
}

then,i use Mirror,

let p1 = Person()
let mirror = Mirror(p1)
println(mirror.typesShortName)

print msg like this:

[String>, Int>, Dog>, Dog>>]

i want,the result is like this:

[String, Int, Dog, [Dog]]

or

[String, Int, Dog, Array]

oh, i need optional property, and optional Array property,can you support it ?? thank you!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.