dennisvennink / MirrorExtensions

A library that adds various Mirror-related operations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MirrorExtensions

Requires Swift > 4 Licensed under the MIT license

MirrorExtensions is a library that adds various Mirror-related operations.

Table Of Contents

Installation

Swift Package Manager

To install this package, add it as a dependency to your project's manifest file (Package.swift), e.g., a package named "Example" whose main product is a library with no dependencies other than MirrorExtensions would be defined as such:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
  name: "Example",
  products: [
    .library(name: "Example", targets: ["Example"])
  ],
  dependencies: [
    .package(url: "https://github.com/dennisvennink/MirrorExtensions.git", from: "1.1.0")
  ],
  targets: [
    .target(name: "Example", dependencies: ["MirrorExtensions"]),
    .testTarget(name: "ExampleTests", dependencies: ["Example"])
  ]
)

Then, import it into your project:

import MirrorExtensions

Contributing

To contribute, think of a missing feature or issue to work on, then fork the project and create your feature branch:

git checkout -b my-new-feature

When you're done implementing your feature, commit your changes:

git commit -am "Add some new feature"

Then push to the feature branch:

git push origin my-new-feature

Finally, submit a pull request!

Testing

All tests are written in XCTest. To perform them, run:

swift test

API

Instance Method Extensions On Mirror

children(_ type:)

Declaration

func children <T> (_ type: T.Type) -> [T]

Description

Creates an Array containing all of the children of self that conform to T.

struct Struct {
  let integer1 = 1
  let boolean1 = true
  let string1 = "a"
  let integer2 = 2
  let boolean2 = false
  let integer3 = 3
}

print(Mirror(reflecting: Struct()).children(Int.self))
// [1, 2, 3]

Parameters

  • type: A static metatype instance.

Returns

An Array containing all of the children of self that conform to T.

descendants(_ type:)

Declaration

func descendants <T> (_ type: T.Type) -> [T]

Description

Creates an Array containing all of the descendants of self that conform to T.

struct Struct2 {
  let integer3 = 3
}

struct Struct1 {
  let integer2 = 2
  let boolean2 = false
  let struct2 = Struct2()
}

struct Struct {
  let integer1 = 1
  let boolean1 = true
  let string1 = "a"
  let struct1 = Struct1()
}

print(Mirror(reflecting: Struct()).descendants(Int.self))
// [1, 2, 3]

Parameters

  • type: A static metatype instance.

Returns

An Array containing all of the descendants of self that conform to T.

About

A library that adds various Mirror-related operations.

License:MIT License


Languages

Language:Swift 100.0%