0

Method swizzling in iOS allows the developer to change the implementation of a type of class at run time. For example, instead of adding code to every view controller, swizzling allows one method to be written, and every view controller will use that method. Is there anything that would result in the same effect when writing code for Android? Write one method that every Activity can use, without having to paste the method into every activity?

For more information on the definition of method swizzling see: http://nshipster.com/method-swizzling/

Alex Wilson
  • 1,773
  • 1
  • 12
  • 18
  • Isn't this just subclassing? Or static methods? – zgc7009 Nov 21 '16 at 20:25
  • No, subclassing an object creates a new class that inherits all of the methods of a superclass and must be instantiated separately. Swizzling allows the programmer to insert a method to the superclass at runtime. This will change the implementation of a class. Thus the programmer only needs to declare a method once for all classes of a certain type to use it. – Alex Wilson Nov 21 '16 at 20:29
  • 3
    Method swizzling involves replacing the implementation of a method with another implementation you create. It's usually used to inject additional functionality into methods in system classes. You wouldn't use method swizzling to just add a method to a class, that is what categories are for. There is no Java equivalent to method swizzling AFAIK. – dan Nov 21 '16 at 20:32
  • Ohhh, so you are saying not where you are literally replacing the functionality of an existing method within the code, not overriding it. Interesting concept. – zgc7009 Nov 21 '16 at 20:39
  • Yes thank you @dan for the clarification – Alex Wilson Nov 21 '16 at 20:40

0 Answers0