Objective-c provides tools that allow us to extend class definitions: categories, extensions, and protocols. Classes: Add new methods to a known class; Extension: notify the use of classes in the definition of this class to declare private methods; Protocols: Protocols declare methods that can be implemented by any class.

1. Category (Category)

1) Declare categories

@interface TestModel (newModel)

-(void)newFun;

@end

This statement has two characteristics. First, the existing class comes after the @Interface keyword, followed by a new name in parentheses. This declaration says that the name of the class is newModel, and that the class will add methods to the TestModel class. You can add as many categories to a class as you want as long as the category names are unique. You can specify the class to which you want to add a category and the name of the category, and you can list the added methods, ending at @end. Unlike the class declaration, there are no instance variable parts and attributes in the class declaration because new implementation variables cannot be added, but the class can access member variables in the original class.

Category limitations

No new instance variables, attributes can be added to the class. There is no room for instance variables in the class.

Second, name conflict, that is, the method in the category and the existing method the same name. When a name conflict occurs, the category takes higher precedence. Your class method will completely replace the original method, so you can no longer use the original method. So add a prefix to your own category method to ensure that no name conflicts occur.

(There can be methods like adding attributes to categories, as shown below)





H to add attributes





M in the set and get method implementation

Quasi-objective function

Classes are used for three main purposes: first, to spread the implementation of a class into different files or frameworks. Second, create a forward reference to the private method. Third, add an informal protocol to the object

Extension

The extension of a class can be seen as an anonymous category, and sometimes a class needs some private methods that it can only see and use. These private methods can be declared by extension, and the methods defined in extension are implemented in the @implementation code area of the class itself.

@interface MyOb : NSObject

{

NSNumber *num;

}

– (NSNumber *)num;

@end

@interface MyOb(Set)

– (void)setNum:(NSNumber *)newNum;

@end

@implementation MyOb

– (NSNumber *)num

{

return num;

}

– (void)setNum:(NSNumber *)newNum

{

//do some thing

}

@end




.m adds private attributes with extensions

When you define an extension without providing a class name, the method defined in the extension is both considered a “must implement” API. In this case, the compiler will warn if the method lacks implementation code, and the implementation of the method must appear in the @implementation block of the class body, the extension doesn’t have its own.m file, Methods must be implemented in the original.m class.

Add categories and extensions to Xcode





new





Select file type

Protocol and proxy patterns

The protocol only declares methods, but does not implement them. The object receiving the protocol is responsible for implementation. OC’s protocol is a list of methods declared by @Protocol and required to be implemented by other classes, equivalent to the @Interface part of the declaration.

Note:

A. The @required modification in the Agreement shall be implemented when confirming the agreement

B. Optionally implement the @optional modifier

C. Use [object conformsToProtocol:@protocol(protocol)] to determine whether to comply with the protocol

D. The protocol is written in the. H file that provides the protocol class

Application of protocol – proxy

The agency model does not do time itself, but requires others to do it.

Category and extension can be used to extend or modify the functionality of an existing class.

Category application is very widespread in iOS, and many classes of the system have multiple category extension functions.

In general categories, you can define new methods, override the original methods of the class, and add readonly attributes

An extension can be considered an anonymous category, but this extension has a special function relative to a category:

[Proprietary] [proprietary] [proprietary] [Proprietary] [Proprietary] [Proprietary] [Proprietary] [Proprietary] [Proprietary] [Proprietary]





The difference between