99import Foundation
1010
1111public protocol ObjectiveKitRuntimeModification {
12-
1312 var internalClass : AnyClass { get }
1413
1514 // MARK: Runtime modification
@@ -26,50 +25,50 @@ public protocol ObjectiveKitRuntimeModification {
2625 /// - Parameters:
2726 /// - identifier: Selector name.
2827 /// - implementation: Implementation as a closure.
29- func addMethod( _ identifier: String , implementation: ImplementationBlock )
28+ @discardableResult
29+ func addMethod( _ identifier: String , implementation: @escaping @convention ( block) ( AnyObject ) -> Void ) -> Selector
3030
3131 /// Exchange selectors implemented in the current class.
3232 ///
3333 /// - Parameters:
3434 /// - aSelector: Selector.
3535 /// - otherSelector: Selector.
3636 func exchangeSelector( _ aSelector: Selector , with otherSelector: Selector )
37-
3837}
3938
4039extension ObjectiveKitRuntimeModification {
41-
4240 public func addSelector( _ selector: Selector , from originalClass: AnyClass ) {
43- guard let method = class_getInstanceMethod ( originalClass, selector) , let implementation = method_getImplementation ( method ) , let typeEncoding = method_getTypeEncoding ( method) else {
41+ guard let method = class_getInstanceMethod ( originalClass, selector) , let typeEncoding = method_getTypeEncoding ( method) else {
4442 return
4543 }
44+ let implementation = method_getImplementation ( method)
4645 let string = String ( cString: typeEncoding)
4746 class_addMethod ( internalClass, selector, implementation, string)
4847 }
4948
50- public func addMethod( _ identifier: String , implementation: ImplementationBlock ) {
51- let blockObject = unsafeBitCast ( implementation, to: AnyObject . self)
49+ @discardableResult
50+ public func addMethod( _ identifier: String , implementation: @escaping @convention ( block) ( AnyObject ) -> Void ) -> Selector {
51+ let blockObject = unsafeBitCast ( implementation, to: NSObject . self)
5252 let implementation = imp_implementationWithBlock ( blockObject)
5353 let selector = NSSelectorFromString ( identifier)
5454 let encoding = " v@:f "
55- class_addMethod ( internalClass, selector, implementation, encoding)
55+ class_replaceMethod ( internalClass, selector, implementation, encoding)
56+ return selector
5657 }
5758
5859 public func exchangeSelector( _ aSelector: Selector , with otherSelector: Selector ) {
59- let method = class_getInstanceMethod ( internalClass, aSelector)
60- let otherMethod = class_getInstanceMethod ( internalClass, otherSelector)
60+ guard let method = class_getInstanceMethod ( internalClass, aSelector) , let otherMethod = class_getInstanceMethod ( internalClass, otherSelector) else {
61+ return
62+ }
6163 method_exchangeImplementations ( method, otherMethod)
6264 }
63-
6465}
6566
6667public extension NSObject {
67-
6868 /// A convenience method to perform selectors by identifier strings.
6969 ///
7070 /// - Parameter identifier: Selector name.
71- public func performMethod( _ identifier: String ) {
71+ func performMethod( _ identifier: String ) {
7272 perform ( NSSelectorFromString ( identifier) )
7373 }
7474}
75-
0 commit comments