Latest 0.2.0
Homepage https://github.com/marmelroy/ObjectiveKit
License MIT
Platforms ios 8.0, requires ARC
Authors

ObjectiveKit - Swift friendly ObjC-Runtime functions

Build Status
Version
Carthage compatible

ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions.

Usage

To use ObjectiveKit:

Import ObjectiveKit at the top of your Swift file:

import ObjectiveKit

The next step is to create an ObjectiveClass object typed for the class you want to modify or introspect:

let viewClass = ObjectiveClass<UIView>()

If using ObjectiveKit on a custom Swift class, make sure that it inherits at some point from NSObject and that it is exposed to the Objective C runtime using the @objc flag.

Introspection

You can learn more about classes at runtime with these handy introspection methods:

let mapViewClass = ObjectiveClass<MKMapView>()
let ivars = mapViewClass.ivars // An array of ivars.
let selectors = mapViewClass.selectors // An array of selectors.
let properties = mapViewClass.properties // An array of properties.
let protocols = mapViewClass.protocols // An array of protocols.

Modifying classes at runtime

Add a pre-existing selector from another class to your ObjectiveClass:

let viewClass = ObjectiveClass<UIView>()
viewClass.addSelector(#selector(testSelector), from: self.classForCoder)
let view = UIView()
view.perform(#selector(testSelector))

Add a custom method by providing the implementation with a closure:

let viewClass = ObjectiveClass<UIView>()
viewClass.addMethod(closureName, implementation: {
    print("hello world")
})
let view = UIView()
view.performMethod(closureName)

ObjectiveKit also supports exchanging selectors in the same class:

let viewClass = ObjectiveClass<UIView>()
viewClass.exchangeSelector(#selector(UIView.layoutSubviews), with: #selector(UIView.xxx_layoutSubviews))

Creating classes at runtime

Lastly, you can also create a custom ObjC class at runtime:

let runtimeClass = RuntimeClass(superclass: UIView.self)
runtimeClass.addIvar(ivarName, type: .Float)
let runtimeObject = runtimeClass.allocate()
runtimeObject.setValue(4.0, forKey: ivarName)

Setting up

Setting up with CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
pod 'ObjectiveKit', '~> 0.2'

Setting up with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

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

github "marmelroy/ObjectiveKit"

Inspiration

Latest podspec

{
    "name": "ObjectiveKit",
    "version": "0.2.0",
    "summary": "Swift friendly API for a set of Objective C runtime functions.",
    "description": "ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions.",
    "homepage": "https://github.com/marmelroy/ObjectiveKit",
    "license": "MIT",
    "authors": {
        "Roy Marmelstein": "[email protected]"
    },
    "source": {
        "git": "https://github.com/marmelroy/ObjectiveKit.git",
        "tag": "0.2.0"
    },
    "social_media_url": "http://twitter.com/marmelroy",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "ObjectiveKit",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This