File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed
Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -22,23 +22,33 @@ Let's say you want to amend `sayHi` from `TestClass`:
2222``` swift
2323
2424class 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
4454Here's what we get when calling ` print(TestClass().sayHi()) `
You can’t perform that action at this time.
0 commit comments