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.