Categories and Inheritance (Inherit) have the same functionality in IOS, but there are differences in some details about the similarities and differences.
Classification can add new methods to this class without knowing the source code of the System class. Categories can only be used to add methods, and cannot add member variables. The system is considered to be part of the class type by the method of increasing the classification . Like what:
1 #import <uikit/uikit.h>< Span style= "color: #008080;" > 2 3 @interface UIImage (WB) 4 5 /* 6 7 */ 8 + (UIImage *) Resizedimagewithname: (nsstring *) name; 9 10 11 @end
UIImage is a class of systems that can still be categorized to add new methods to the class without knowing the source code of the UIImage class. And the method becomes part of that type. If other classes inherit from the UIImage class, the subclasses of the UIImage class all have the Resizedimagewithname method.
From this perspective, there is a big difference between inheritance and classification: subclasses inherit from the parent class, and the added methods in the subclass do not become part of the parent class . That is, if Class A inherits from UIImage, a resizedimage function is added to Class A, and the parent class UIImage cannot use the function, and when there are new classes such as Class B inherits from UIImage, there is no resizedimage function in class B.
However, inheritance can add both methods and member variables. Of course, the member variables that are added are also limited to being used in subclasses.
IOS Classification and inheritance