Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marmelroy/objectivekit
Swift-friendly API for a set of powerful Objective C runtime functions.
https://github.com/marmelroy/objectivekit
introspect runtime swift
Last synced: 4 days ago
JSON representation
Swift-friendly API for a set of powerful Objective C runtime functions.
- Host: GitHub
- URL: https://github.com/marmelroy/objectivekit
- Owner: marmelroy
- License: mit
- Created: 2016-11-13T20:16:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-22T01:49:21.000Z (over 4 years ago)
- Last Synced: 2025-02-15T08:49:47.329Z (4 days ago)
- Topics: introspect, runtime, swift
- Language: Swift
- Homepage:
- Size: 45.9 KB
- Stars: 849
- Watchers: 14
- Forks: 36
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.org/marmelroy/ObjectiveKit)
[](http://cocoapods.org/?q=ObjectiveKit)
[](https://github.com/Carthage/Carthage)# 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:
```swift
import ObjectiveKit
```The next step is to create an ObjectiveClass object typed for the class you want to modify or introspect:
```swift
let viewClass = ObjectiveClass()
```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:
```swift
let mapViewClass = ObjectiveClass()
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:
```swift
let viewClass = ObjectiveClass()
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:
```swift
let viewClass = ObjectiveClass()
viewClass.addMethod(closureName, implementation: {
print("hello world")
})
let view = UIView()
view.performMethod(closureName)
```ObjectiveKit also supports exchanging selectors in the same class:
```swift
let viewClass = ObjectiveClass()
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:
```swift
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](http://cocoapods.org/?q=ObjectiveKit)
```ruby
source 'https://github.com/CocoaPods/Specs.git'
pod 'ObjectiveKit', '~> 0.2'
```### Setting up with Carthage
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```To integrate ObjectiveKit into your Xcode project using Carthage, specify it in your `Cartfile`:
```ogdl
github "marmelroy/ObjectiveKit"
```### Inspiration
- [https://github.com/mikeash/MAObjCRuntime](https://github.com/mikeash/MAObjCRuntime)