12-Extension Extension

1. Extension: Extension
1). Is a special category. So extension is also part of the class.
2). Special features:
a. Extending this particular category has no name.
b. Only the declaration has no implementation. It shares an implementation with this class.
2. Extended syntax
grammar;
@interface classname()

@end
No implementation. Shares 1 implementation with this class.
3. Steps to add extensions to the class
There is only 1 .h file. File name: class name_get file name.h
There are only extended declarations in this file.
@interface Person ()
@end
4. Basic use of extension.
1). The essence of extension is a classification. As part of this class.
Just a special category
no name.
2). Extensions only have declarations, no separate implementations. Share an implementation with this class.
5. The difference between extension and classification
1). Categories have names. Extending without names is an anonymous category.
2). Each category has a separate declaration and implementation. The extension only declares that there is no separate implementation and this class shares one implementation,
3). Only new methods can be added to the category. Any member of the extension can be written.
4). @property can be written in the category but only the declaration of the getter setter will be generated.
Writing @property in the extension will automatically generate private properties and also generate getter setter declarations and implementations.
6. Extended application scenarios.
1). To write a private @property for the class.
The generated getter and setter methods can only be accessed inside the class and cannot be accessed outside.
In fact, we can think: @property generates private properties, generates getter setter implementations, and does not declare them.
2). In the case of 100% extension, it will not monopolize 1 file. The extension is directly written in the implementation file of this class.
At this time, the members written in the extension are equivalent to the private members of this class. They can only be accessed in the implementation of this class.
Not accessible from outside.
3). When to use extension?
Extensions can be used when we want to define private members for a class. The extension is defined in the implementation file of this class.
If you want to write a true private property for the class, although we can define it in @implementation. But don't write it like this is very irregular.
Write an extension. Define this private property in the extension.
If you want to write a private method for a class, it is recommended to write the declaration in the extension and the implementation in the implementation of this class. Provide code readability
If you want to write a private @property for the class, you can write it directly in the extension.
4). Extensions are built to privatize members of a class.
If the members of the class only want to be accessed inside the class, then define it in the extension.
If the members of the class are allowed to be accessed by the outside world, they are defined in the @interface of this class.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901640&siteId=291194637