iOS has launched a category (Category) in 2.0, which allows developers to extend the class without altering the original class. We know that both OC and Swift have a single inheritance attribute, or you can implement a method extension of a class by inheriting the parent class by creating a new subclass. But where is the difference between the two? If you rewrite a class, it's better to use a category or inheritance.
To avoid repetition of wheel-making, direct conclusions are invoked:
In the following cases, the method of using inheritance: 1 to extend the new extension has the same name as the original method , but the implementation of the parent class is also required.
2 extends the properties of the class .
ViewControllerEx.h
@interfaceViewControllerEx: Uiviewcontroller
//I need to add the method
@end
//
Implementation of the VIEWCONTROLLEREX.M @implementationViewControllerEx//Method
@end
use categories in the following situations:1 for system-specific classes, such as:
Nsstring,nsarray,nsnumber and so on.
2 for a custom class, for large and complex classes , to improve maintainability, the related methods are grouped into separate files.
@interface Main class class name (category class name)
//You cannot define member properties
@end
@implementation The main class class name (the category class name)
@end
There is a conventional rule that when a class file is named, it is the original class name + extension ID
// nsstring+ex.h
@interface nsstring (ex)
/Extended Class-back method
@end
//
implementation of NSSTRING+EX.M @implementation NSString (ex)
/method
@end
3 Although it is not possible to define member properties in category (category), there are ways to support adding properties and member variables
A common approach is to access and generate associated objects through Objc_getassociatedobject/objc_setassociatedobject in Runtime.h. This method is used to simulate the build properties.
"Nsobject+specialname.h" file:
@interface nsobject (specialname)
@property (nonatomic, copy) NSString * specialname;
@end
"NSOBJECT+SPECIALNAME.M" File:
#import "Nsobject+extension.h"
#import <objc/runtime.h>
Static Const VOID*SPECIALNAMEKEY = &SpecialNameKey;
@implementation NSObject (specialname)
@dynamic specialname;
-(NSString *) specialname {
//If the property value is a non-ID type, you can construct the OC ID object first by the attribute value and then get the non-ID type property return via the object
Objc_getassociatedobject (self, specialnamekey);
}
-(void) Setspecialname: (NSString *) specialname{
//If the property value is a non-ID type, you can construct the OC ID object first by the property value and then get the non-id type attribute through the object
objc_ Setassociatedobject (self, specialnamekey, specialname, objc_association_retain_nonatomic);
@end
4 The priority of the method in the attention item classification is higher than the method in the original class. That is, the methods in the original class are overridden in the taxonomy, and the methods in the taxonomy override the method in the original class to declare only the method, cannot add the property variable, and the method in the Run-time classification is not different from the method in the main class. The classification is defined in the. h file, but it can also be defined in the. m file, at which point the method of categorization becomes a private method what is a class extension (extensions). Click to open the link
? There is a conventional rule that when a class file is named, it is the original class name + extension ID//nsstring+ex.h @interface NSString (ex)//Extended Class-back method @end//NSSTRING+EX.M @implemen Implementation of Tation NSString (ex)/method @end
Reference: http://bluevt.org/?p=183