[Objective-C] class type, class Method

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.