iosswiftmethod-swizzling

Is it possible to disable method swizzling in a swift project?


I have a very large project with 3rd party frameworks and would like to disable method swizzling to make sure the 3rd party frameworks do not mess the default expected behaviour. Is it possible? Is there some flag in the project settings?


Solution

  • There is no option to disable that although if you avoid subclassing from NSObject you would be safe . If you want to prevent Any Library from swizzling you may override the core methods for it namely

    public func class_addMethod(_ cls: Swift.AnyClass!, _ name: Selector!, _ imp: IMP!, _ types: UnsafePointer<Int8>!) -> Bool

    and

    public func method_exchangeImplementations(_ m1: Method!, _ m2: Method!)

    Like this

    public func method_exchangeImplementations(_ m1: Method!, _ m2: Method!) {
     }
    public func class_addMethod(_ cls: Swift.AnyClass!, _ name: Selector!, _ imp: IMP!, _ types: UnsafePointer<Int8>!) -> Bool
     {
     return false
    }
    

    I do not suggest that though , but you might give it a try.