Skip to content
A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.
Branch: master
Clone or download
Latest commit 492fc16 Jun 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Resources resources folder Nov 6, 2017
Runtime.xcodeproj
Scripts renamed script Dec 15, 2017
Sources/Runtime check the flags to check whether the type is generic or not Apr 24, 2019
Tests Merge pull request #47 from noahemmet/swift51 Jun 13, 2019
.gitignore
.swift-version set swift-version to 5 Apr 18, 2019
.swiftlint.yml Centralization and enum support Apr 6, 2019
.travis.yml Centralization and enum support Apr 6, 2019
LICENSE Initial commit Nov 4, 2017
Package.resolved update swift_allocObject call for void * Apr 18, 2019
Package.swift update swift_allocObject call for void * Apr 18, 2019
Podfile update cocoapods Apr 29, 2019
README.md Update README.md Apr 19, 2019
Runtime.podspec update cocoapods Apr 29, 2019

README.md

Runtime

Build Swift 5.0 CocoaPods compatible License

Runtime is a Swift library to give you more runtime abilities, including getting type metadata, setting properties via reflection, and type construction for native swift objects.

TypeInfo

TypeInfo exposes metadata about native Swift structs, protocols, classes, tuples and enums. It captures the properties, generic types, inheritance levels, and more.

Example

Lets say you have a User struct:

struct User {
  let id: Int
  let username: String
  let email: String
}

To get the TypeInfo of User, all that you have to do is:

let info = try typeInfo(of: User.self)

Property Info

Within the TypeInfo object, it contains a list of PropertyInfo which represents all properties for the type. PropertyInfo exposes the name and type of the property. It also allows the getting and setting of a value on an object.

Example

Using the same Person object as before first we get the TypeInfo and the property we want.

let info = try typeInfo(of: User.self)
let property = try info.property(named: "username")

To get a value:

let username = try property.get(from: user)

To set a value:

try property.set(value: "newUsername", on: &user)

It's that easy! 🎉

Factory

Runtime also supports building an object from it's Type. Both structs and classes are supported.

To build a User object:

let user = try createInstance(type: User.self)

Function Info

FunctionInfo exposes metadata about functions. Including number of arguments, argument types, return types, and whether it can throw an error.

Example

func doSomething(a: Int, b: Bool) throws -> String { 
  return "" 
}

let info = functionInfo(of: doSomething)

FAQ

Q: When getting and setting a value does it work typeless? (i.e. object casted as Any)

A: Yes! The whole library was designed with working typeless in mind.

Q: When creating a new instance of a class is it still protected by ARC?

A: Yes! The retain counts are set properly so ARC can do its job.

Installation

Cocoapods

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

pod 'Runtime'

Swift Package Manager

You can install Runtime via Swift Package Manager by adding the following line to your Package.swift:

import PackageDescription

let package = Package(
    [...]
    dependencies: [
        .Package(url: "https://github.com/wickwirew/Runtime.git", majorVersion: XYZ)
    ]
)

Contributions

Contributions are welcome and encouraged!

Learn

Want to know how it works? Here's an article on how it was implemented.

Want to learn about Swift memory layout? Mike Ash gave and awesome talk on just that.

License

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

You can’t perform that action at this time.