Nsclassfromstring of iOS work notes

Source: Internet
Author: User

ID MYOBJ = [[Nsclassfromstring (@ "Myspecialclass") alloc] init];

And

ID MYOBJ = [[Myspecialclass alloc] init];

is the same. However, if you do not have the Myspecialclass class in your program, the following is an error, and the above notation simply returns an empty object.

Therefore, in some cases, you can use Nsclassfromstring to initialize classes that you are unsure about.

For example, in the iphone, Nstask may be the case, so when you need to use Nstask, it's best to use:

[[Nsclassfromstring (@ "Nstask") ...]

Instead of directly using [Nstask ...] This notation.

The benefits of nsclassfromstring are:

1 weakens the connection, and therefore does not link the absence of the framework to the program.

2 Import is not required because the class is dynamically loaded and can be loaded as long as it exists. So if you do not have a header file definition for a class in your toolchain, and you are sure that the class is available, you can use this method.

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.