Category: Extending the built-in functionality of existing classes (the ability to extend classes without inheritance)
A category can extend a property that does not exist before the class, and cannot add instance variables to the class interface as inherited, but rather extends the behavior of a class
In addition to adding new behavior to existing classes, categories also support grouping related methods into multiple separate files for classes that you build
The privatization of methods or properties can be implemented with anonymous classes in M files
Add after import:
Interface class name () {
Privatization properties
}
Protocol: A protocol defines a priori way for a class to communicate with another class, which contains a list of methods, either optional or mandatory. The methods defined in the protocol include some operation responses and event responses. Both the delegate and the data source are generated by the protocol.
A protocol is similar to an interface in Java, and defines a set of methods that the classes that follow the agreement agree to satisfy this set of methods list
Protocols can be used to define callback methods (both iOS delegates and data sources are implemented using protocols, Uitableviewdelegate, Uitableviewdatasource)
The protocol is divided into the optional implementation method and the necessary implementation method, which is differentiated by keyword @required and @optional
An optional way to determine whether a class implements the protocol:
[Java]View Plaincopyprint?
- if ([self.client respondstoselector: @selector (dosomething)])
- {
- [Self.client dosomething];
- }