MirrorExtensions is a library that adds various Mirror
-related operations.
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
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!
All tests are written in XCTest. To perform them, run:
swift test
func children <T> (_ type: T.Type) -> [T]
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]
type
: A static metatype instance.
An Array
containing all of the children of self
that conform to T
.
func descendants <T> (_ type: T.Type) -> [T]
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]
type
: A static metatype instance.
An Array
containing all of the descendants of self
that conform to T
.