4 ms·
Message swizzling/monkey patching is a hack that tries to deal with the reality of not being able to modify base classes you don't have the source code for and
by mahyarm 12y ago
Message swizzling/monkey patching is a hack that tries to deal with the reality of not being able to modify base classes you don't have the source code for and creates all sorts of surprise bugs.
If objective-c had proper support to do it via categories that can 'over-ride' methods it would be a lot cleaner in general.
- emp 12y agoBut it does allow for programmers to add their own ideas. I have a traits implementation where I can inject entire behaviours in a line of code. I am not sure if this could be done in Swift (haven't had time to research properly).
- mahyarm 12y agoThat's what I mean by implemented better as something that is officially supported. In objective-c it's a hack, it can lead to hidden surprises since code usually doesn't make it obvious that a specific method has been swizzled. And you have to deal with hidden expectations in code that you have no control over.
- emp 12y agoTrue, though then we are waiting and hoping for features. Another example of some runtime fun: my debug build swizzles interceptors for common UI lifecycle and action methods (viewDidLoad, didMoveToParentViewController:, sendAction:to:forEvent:, etc). When I am working on an app with many views, a simple glance at the console shows me what view controller displayed, what action a button fired and so on. Great for getting to relevant code quickly. I don't think I could hope for this as a standard feature of a language.