The following are the notes for learning objective-C 2.0. The original book I purchased is the original English version. Due to my limited level of English, there may be omissions in understanding.
Original book purchase address: Amazon
4. Protocols and categories
Item 23: Use delegate and Data Source protocols for interobject Communication
Some knowledge points:
(1) A protocol name is usually followed by a delegate.
(2) A delegate is usually declared in the class continuation category.
(3) For proxy methods with optional attributes, you must use the introspection mode to determine whether the proxy method is implemented.
(4) It is recommended that a proxy method include its own object as a parameter, so as to easily determine which object the proxy method is called.
(5) reasonably use _ delegateflags.
Remember:
(1) Use the proxy mode to provide an interface for objects that need to know other object-related events.
(2) The defined proxy method must contain all possible proxy methods and provide optional attributes. (3) define the data source class proxy to provide interfaces for the object to obtain data of other objects. (4) If necessary, implement the bitwise structure method to determine which methods are implemented in the proxy.
Item 24: use categories to break class implementations into manageable segments
Remember:
(1) use categories to divide a category into multiple fragment classes that are easy to manage. (2) create a category called private, and place the hidden private method names in this category for calling reference.
Item 25: Always prefix category names on third-party classes
Some knowledge points:
(1) The method added in the category will overwrite the Same Name method in the original class and the same name method in other categories.
Remember:
(1) Remember to add a naming prefix to add a category for a class that is not compiled by yourself. (2) You must remember to add a naming prefix for classes that are not compiled by yourself.
Item 26: Avoid properties in categories
Remember:
(1) ensure that all attributes are defined in the main interface definition. (2) unless it is a class extension attribute, it is best to select a read method in the category instead of defining the attribute to get data.
Item 27: Use the class-continuation category to hide implementation detail
Some knowledge points:
(1) directly modifying the attributes of an object triggers KVO notifications. If you do not want to trigger KVO, you can modify the attributes using a certain modification method.
Remember:
(1) Add sample variables of a class through the class extension category. (2) Redefine the writable attribute of an attribute in the class extension category. If this attribute is read-only for external interfaces, it must be writable internally. (3) Define private methods in the class extension category. (4) Define private protocols in the class extension category.
Item 28: Use a protocol to provide anonymous objects
Remember:
(1) protocols can be used to provide different levels of anonymous types. These types can be summarized into an ID type that implements Protocol attributes and methods. (2) Use an anonymous object to hide a type of name. (3) using anonymous objects when a type name is not particularly important, and some defined methods in the type are more important.