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

Overview

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

You might also like...
DGLabelSize - Functions that calculate the size of uilabel based on different string lengths
DGLabelSize - Functions that calculate the size of uilabel based on different string lengths

DGLabelSize Functions that calculate the size of uilabel based on different stri

Handy Combine extensions on NSObject, including Set<AnyCancellable>.
Handy Combine extensions on NSObject, including SetAnyCancellable.

Storable Description If you're using Combine, you've probably encountered the following code more than a few times. class Object: NSObject { var c

Automatically set your keyboard's backlight based on your Mac's ambient light sensor.
Automatically set your keyboard's backlight based on your Mac's ambient light sensor.

QMK Ambient Backlight Automatically set your keyboard's backlight based on your Mac's ambient light sensor. Compatibility macOS Big Sur or later, a Ma

 Extendy - A set of useful string extensions.
Extendy - A set of useful string extensions.

Extendy A set of useful string extensions. Requirements iOS 11.0+ Swift 5+ Installation CocoaPods Extendy is available through CocoaPods. To install i

Simple way to set up half modal view
Simple way to set up half modal view

HalfModalView Requirements iOS 9.0+ Xcode 10.0+ Swift 4.0+ Example Installation CocoaPods is a dependency manager for Cocoa projects. You can install

A set of utilities (vmcli + vmctl) for macOS Virtualization.framework

VMCLI A set of utilities to help you manage VMs with Virtualization.framework Installation Prerequisites macOS Big Sur (11+) XCode.app installed # mak

🗃 Powerful and easy to use Swift Query Builder for Vapor 3.
🗃 Powerful and easy to use Swift Query Builder for Vapor 3.

⚠️ This lib is DEPRECATED ⚠️ please use SwifQL with Bridges Quick Intro struct PublicUser: Codable { var name: String var petName: String

A Powerful , Extensible CSS Parser written in pure Swift.
A Powerful , Extensible CSS Parser written in pure Swift.

A Powerful , Extensible CSS Parser written in pure Swift.

Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX)
Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX)

Mission Control Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX) Brought to you by Have you ever wished you could chang

Comments
  • Properties instead of methods?

    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

    enhancement 
    opened by icanzilb 1
  • More swifty naming ?

    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 ?

    opened by alikaragoz 1
  • Swift5

    Swift5

    Nice to meet you. I am Koutarou Ishikawa, Japanese application engineer. I am trying to do a "programming-shakyo" on the framework PXSourceList: porting it from Objective-C to Swift.

    When I compiled "ObjectiveKit" with Xcode 11, there were some errors in it. So I fixed it.

    • Changes list:
      • Swift version: from 3 to 5.

      • The location of definition performMethod(_:): Moved from RuntimeModification.swift to NSObject.swift. NSObject.swift into added group 'Extensions', without Folder. RuntimeModification.swift into added group 'Protocols', without Folder.

      • Changed the implementation location of the following methods to ObjectiveClass and RuntimeClass: addSelector(_:from:), addMethod(_:implementation:), and exchangeSelector(_:implementation:).

      • The parameter implementation of addMethod(_:implementation:): correctted from ImplementationBlock to @escaping @convention(block) () -> Void.

      • The sources of variable unwrapped in ivars, selectors, protocols, and properties on ObjectiveClass: correctted from ivarList?[i].unsafelyUnwrapped to propertyList![i].self.

      • Moved build.sh, LICENSE, ObjectiveKit.podspec, README.md into added group 'Supporting Files', without Folder.

      • Since only calling MapKit in the Test class, added the MapKit.framework to the target "ObjectiveKit". However, I personally think not correct that adding MapKit.framework to ObjectiveKit.xcodeproj in order to pass 'testIntrospection()'.

    opened by stein2nd 0
  • Removed Duplication in ObjectiveClass

    Removed Duplication in ObjectiveClass

    The code was created for the sole purpose of showing my students, that they should think on their own instead of following thought leaders and popular bloggers, as they, while being popular, still are human and sometimes don't write that great or just plain bad and dirty code. The code prior to the pull request is a perfect example of such code because of the excess amount of duplication.

    The secondary aim of refactoring was to show my students, how to use some unobvious ways of handling functions in swift, when using unsafe force-cast related code.

    I've created this pull request just to check, how hacktoberfest (I really want that t-shirt, yay) reacts to rejected or ignored pull requests, as original maintainer, from what it looks, ignores pull requests from other users.

    P.S. I doubt the maintainer would merge the pull request, so I didn't update to Swift 4. If accept happens for some weird reason, I would refactor it to swift 4 with pleasure.

    opened by trimmurrti 3
Releases(0.1.0)
Owner
Roy Marmelstein
Ya tu sabes.
Roy Marmelstein
💡 A light Swift wrapper around Objective-C Runtime

A light wrapper around Objective-C Runtime. What exactly is lumos? lumos as mentioned is a light wrapper around objective-c runtime functions to allow

Suyash Shekhar 139 Dec 19, 2022
A set of helper classes and functions in Swift

SwiftTools This is a set of tools written in Swift that add some sugar and some small functionality. Mainly meant for small projects and scripts, as a

Vinicius Vendramini 0 Dec 13, 2021
Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway

Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway deployed on Graviton arm64 build swift:5.6.2-amazonlinux2-docker image

Furqan 2 Aug 16, 2022
Plugin and runtime library for using protobuf with Swift

Swift Protobuf Welcome to Swift Protobuf! Apple's Swift programming language is a perfect complement to Google's Protocol Buffer ("protobuf") serializ

Apple 4.1k Dec 28, 2022
A simple utility allowing to detect Swift version at runtime.

SwiftVersionDetector SwiftVersionDetector allows you to detect Swift version at runtime. Note that detecting the Swift version of the machine on which

Alessandro Venturini 2 Dec 3, 2022
Infix operators for monadic functions in Swift

Indecipherable symbols that some people claim have actual meaning. Please see the documentation for installation instructions. What's included? Import

thoughtbot, inc. 825 Dec 7, 2022
Call Swift functions from Rust with ease!

swift-rs Call Swift functions from Rust with ease! Todo Swift class deallocation from rust (implementing Drop and using deallocate_{type} methods) Mor

null 69 Dec 30, 2022
Useful functions and extensions for sorting in Swift

SwiftSortUtils Motivation This library takes a shot at making comparing and sorting in Swift more pleasant. It also allows you to reuse your old NSSor

Daniel Strittmatter 60 Sep 9, 2022
Functional data types and functions for any project

Swiftx Swiftx is a Swift library containing functional abstractions and extensions to the Swift Standard Library. Swiftx is a smaller and simpler way

TypeLift 219 Aug 30, 2022
Utility functions for validating IBOutlet and IBAction connections

Outlets Utility functions for validating IBOutlet and IBAction connections. About Outlets provides a set of functions which validate that IBOutlets ar

Ben Chatelain 129 May 2, 2022