Skip to content

Commit fa1e1d3

Browse files
committed
Present undo
1 parent 47e83c1 commit fa1e1d3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,33 @@ Let's say you want to amend `sayHi` from `TestClass`:
2222
```swift
2323

2424
class TestClass: NSObject {
25+
// Functions need to be marked as `@objc dynamic` or written in Objective-C.
2526
@objc dynamic func sayHi() -> String {
2627
print("Calling sayHi")
2728
return "Hi there 👋"
2829
}
2930
}
3031

31-
try Interpose(TestClass.self) {
32+
let interposer = try Interpose(TestClass.self) {
3233
try $0.hook(#selector(TestClass.sayHi), { store in { `self` in
34+
3335
print("Before Interposing \(`self`)")
3436

35-
let string = store((@convention(c) (AnyObject, Selector) -> String).self)(`self`, store.selector)
37+
// Calling convention and passing selector is important!
38+
// You're free to skip calling the original implementation.
39+
let origCall = store((@convention(c) (AnyObject, Selector) -> String).self)
40+
let string = origCall(`self`, store.selector)
3641

3742
print("After Interposing \(`self`)")
43+
3844
return string + testSwizzleAddition
39-
}
40-
as @convention(block) (AnyObject) -> String})
45+
46+
// Similar signature cast as above, but without selector.
47+
} as @convention(block) (AnyObject) -> String})
4148
}
49+
50+
// Don't need the hook anymore? Undo is built-in!
51+
interposer.revert()
4252
```
4353

4454
Here's what we get when calling `print(TestClass().sayHi())`

0 commit comments

Comments
 (0)