We can always learn a lot from interview questions and understand more about basics.
- Customize existing class
Categories add methods to existing classes
Class Extensions Extend the Internal Implementation (can add property)
Create a subclass and override methods
Use delegate object
Add associative references to an object
1)Swizzling should always be done in +load.
+load is guaranteed to be loaded during class initialization, which provides a modicum of consistency for changing system-wide behavior.
+initialize is called just before the application calls its first method on that class or an instance of that class. Both are optional, and are executed only if the method is implemented.
2)Swizzling should always be done in a dispatch_once.
The best way to understand the relationship between these concepts is as follows: a class (
Class) maintains a dispatch table to resolve messages sent at runtime; each entry in the table is a method (
Method), which keys a particular name, the selector (
SEL), to an implementation (
IMP), which is a pointer to an underlying C function.
To swizzle a method is to change a class’s dispatch table in order to resolve messages from an existing selector to a different implementation, while aliasing the original method implementation to a new selector.
1)Always invoke the original implementation of a method (unless you have a good reason not to)
3)Understand what’s going on
4)Proceed with caution