Class type
So far, we have known the SEL data type corresponding to the method. Like Sel, in objective-C, we can not only use the SEL corresponding to the method, for Classes in objective-C, a similar mechanism is also provided for us, class type. After a class is correctly compiled, a variable exists in the successfully compiled class to save the class information. We can get this class through a normal string or through the object we generate. After the class is successfully obtained, we can use it as a defined class. This mechanism allows us to obtain the class of the object in the Process of program execution, or dynamically generate an object that cannot be determined in the compilation stage during the program execution stage.
Because the class stores all the information of a class, of course, we can also obtain a class superclass. The usage format of the class type is as follows:
1 class variable name = [class or object class];
2 class variable name = [class or object superclass];
3 class variable name = nsclassfromstring (method name string );
4 nsstring * variable name = nsstringfromclass (class parameter );
The first line of code is to obtain the class variable of this class or object by sending a class message to a class or object.
The second line of code is to obtain the class variable of this class or object by sending a superclass message to a class or object.
The third line of code is to call the nsclassfromstring function and use a string as a parameter to obtain the class variable. When we use the configuration file to determine the class for execution, nsclassfromstring brings us great convenience.
The fourth line of code is the nsstringfromclass Inverse Function of nsclassfromstring. A class type is used as a variable to get the name of a class.
When we use the first, second, or third lines of code in the program to successfully obtain a class variable, for example, we name this variable myclass, in future code, we can use myclass as a defined class, of course, we can pass this variable as a parameter to other methods so that other methods can dynamically generate the objects we need.