GithubHelp home page GithubHelp logo

marmelroy / objectivekit Goto Github PK

View Code? Open in Web Editor NEW
848.0 14.0 37.0 47 KB

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

License: MIT License

Swift 78.67% Objective-C 4.01% Ruby 11.87% Shell 5.45%
swift introspect runtime

objectivekit's Introduction

ObjectiveKit - Swift friendly ObjC-Runtime functions

Build Status Version Carthage compatible

ObjectiveKit

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

objectivekit's People

Contributors

marmelroy 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

objectivekit's Issues

Properties instead of methods?

I think in current trends dynamic properties are favorited over methods without parameters, e.g. you have let ivars = mapViewClass.ivars() this could be simply mapViewClass.ivars Just throwing the idea on the table

More swifty naming ?

This is not an exhaustive list of course but I think this project should benefit from more concise and compact naming ร  la Swift 3.0.

Before After
let ivars = mapViewClass.allIvars() let ivars = mapViewClass.vars()
let selectors = mapViewClass.allSelectors() let selectors = mapViewClass.selectors()
let properties = mapViewClass.allProperties() let properties = mapViewClass.properties()
let protocols = mapViewClass.allProtocols() let protocols = mapViewClass.protocols()
viewClass.addSelectorToClass(...) viewClass.addSelector(...)
viewClass.addMethodToClass(...) viewClass.addMethod(...)

I've spotted a few other places where this can be applied. What do you think ?

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.