6
6
[ ![ Carthage compatible] ( https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat )] ( https://github.com/Carthage/Carthage )
7
7
8
8
# ObjectiveKit
9
- ObjectiveKit provides a Swift- friendly API for a set of powerful Objective C runtime functions.
9
+ ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions.
10
10
11
11
## Usage
12
12
@@ -39,17 +39,15 @@ let protocols = mapViewClass.allProtocols() // Returns an array of protocols.
39
39
40
40
### Modifying classes at runtime
41
41
42
- ObjectiveKit to add / exchange methods for a class at runtime.
43
-
44
- You can add a pre-existing function from another class to your Objective class:
42
+ Add a pre-existing function from another class to your Objective class:
45
43
``` swift
46
44
let viewClass = ObjectiveClass< UIView> ()
47
45
viewClass.addSelectorToClass (#selector (testSelector), fromClass : self .classForCoder )
48
46
let view = UIView ()
49
47
view.perform (#selector (testSelector))
50
48
```
51
49
52
- Alternatively, you can add a method by providing a custom implementation with a closure:
50
+ Add a method by providing a custom implementation with a closure:
53
51
``` swift
54
52
let viewClass = ObjectiveClass< UIView> ()
55
53
viewClass.addMethodToClass (closureName, implementation : {
@@ -65,6 +63,16 @@ let viewClass = ObjectiveClass<UIView>()
65
63
viewClass.exchangeSelector (#selector (UIView.layoutSubviews ), with : #selector (UIView.xxx_layoutSubviews ))
66
64
```
67
65
66
+ ### Creating classes at runtime
67
+
68
+ Lastly, you can also create a custom ObjC class at runtime:
69
+ ``` swift
70
+ let runtimeClass = RuntimeClass (superclass : UIView.self )
71
+ runtimeClass.addIvar (ivarName, type : .Float )
72
+ let runtimeObject = runtimeClass.allocate ()
73
+ runtimeObject.setValue (4.0 , forKey : ivarName)
74
+ ```
75
+
68
76
## Setting up
69
77
70
78
### Setting up with [ CocoaPods] ( http://cocoapods.org/?q=ObjectiveKit )
0 commit comments