Giter Site home page Giter Site logo

runtimekit's Introduction

RuntimeKit

Build Status Swift 3.1 CocoaPods CocoaPods CocoaPods

RuntimeKit is a Swift library for accessing the Objective-C runtime. In addition to providing wrappers around features like method swizzling or associated values, it also provides some type safety.

As of right now, RuntimeKit supports method swizzling, replacing a methods implementation with a block, setting associated objects, looking up an object's properties, adding new methods to existing classes and creating new classes and protocols. More features will be added over time.

Installation

CocoaPods (recommended)

pod 'RuntimeKit'

Build from source

Just copy everything in RuntimeKit/Sources into your Xcode project

Features:

  1. Method swizzling
  2. Associated objects
  3. Creating new classes
  4. Creating new protocols
  5. Performing ObjC Selectors w/ some additional type safety

Method Swizzling

Switching two method's implementations

extension NSDate {
    func xxx_addingTimeInterval(_ ti: TimeInterval) -> NSDate {
        print("will add \(ti)")
        return self.xxx_addingTimeInterval(ti)
    }   
}

try! NSDate.swizzle(#selector(NSDate.addingTimeInterval(_:)), with: #selector(NSDate.xxx_addingTimeInterval(_:)))

NSDate().addingTimeInterval(100) // prints "will add 100"

Replacing a method's implementation with a block

let systemFontOfSize_block: @convention(block) (UIFont, Selector, CGFloat) -> UIFont = { (self, sel, size) in
    return UIFont(name: "Avenir-Book", size: size)!
}

try! UIFont.replace(#selector(UIFont.systemFont(ofSize:)), withBlock: systemFontOfSize_block, methodType: .class)

let systemFont = UIFont.systemFont(ofSize: 15) // -> <UICTFont: 0x7fddc5703150> font-family: "Avenir-Book"; font-weight: normal; font-style: normal; font-size: 15.00pt>

Associated objects

extension AssociatedObjectKeys {
    static let name = AssociatedObjectKey<String>("name")
    static let age  = AssociatedObjectKey<Int>("age")
}

class Person: NSObject {}

let me = Person()
me.setAssociatedObject("Lukas", forKey: .name)
me.setAssociatedObject(18, forKey: .age)

let name = me.getAssociatedObject(forKey: .name) // Optional("Lukas")
let age  = me.getAssociatedObject(forKey: .age)  // Optional(18)

Creating new classes

let LKGreeter = try! Runtime.createClass("LKGreeter")

let greetMethod_block: @convention(block) (NSObject, Selector, String) -> String = { (_self, _sel, name) in
    return "Hello, \(name)"
}

try! LKGreeter.addMethod("greet", implementation: greetMethod_block, methodType: .class, returnType: .object, argumentTypes: [.object, .selector, .object])

LKGreeter.perform("greet", with: "Lukas").takeRetainedValue() // result: "Hello, Lukas"

Creating new protocols

let methods = [
    ObjCMethodDescription("greet", returnType: .object, argumentTypes: [.object, .selector, .object], methodType: .class, isRequired: true)
]

let GreeterProtocol = try! Runtime.createProtocol("Greeter", methods: methods)
let LKGreeter = try! Runtime.createClass("LKGreeter", superclass: NSObject.self, protocols: [GreeterProtocol])

Performing Objective-C Selectors with some additional type safety

let formatBlock: @convention(block) (NSDate, Selector, String) -> String = { (_self, _sel, format) in
    let formatter = DateFormatter()
    formatter.dateFormat = format
    return formatter.string(from: _self as Date)
}
try! NSDate.addMethod("customFormat:", implementation: formatBlock, methodType: .instance, returnType: .object, argumentTypes: [.object, .selector, .object])

extension ObjCMethodCallRequests {
    static let customFormat = ObjCMethodCallRequest<String>("customFormat:")
}

let now = NSDate()
let formatString: String = try! now.perform(.customFormat, "EEEE MMM d, yyyy") // -> "Saturday Apr 1, 2017"

runtimekit's People

Contributors

lukaskollmer 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

Watchers

 avatar  avatar  avatar  avatar

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.