开发者

need advise about NSClassFromString [closed]

开发者 https://www.devze.com 2022-12-23 00:11 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center f开发者_如何学运维or guidance. Closed 9 years ago.

according to this code i dont understand about NSClassFromString

how did i know "viewControllerName" can i use it from another source ?

could you please tell me more information how to use NSClassFromString?

UIViewController *viewController = [[NSClassFromString(viewControllerName) alloc] init];


NSClassFromString returns an Objective-C class based on it's name as a string (The function name is really self explanitory)...

The string you pass in is the name of the class you want to create, for example:

If you have a class called MyViewController and you want to get an Objective-C class from that string, you use this function, like NSClassFromString(@"MyViewController");

Normally you only need to do this when testing for class names that may not be available to you, for example, when writing code that targets iPhone OS 2.0 and 3.0 simultaneously.

If you use the class as a symbol, it'll crash if the class doesn't exist on the previous version of the OS. So you use this function to return a class using that string, and if the class is successfully returned, you know that it exists and is safe to use, else if it returns nil, then the class doesn't exist, you shouldn't use it and your program should take steps to behave properly in this situation.


I have a situation in which I wish the class name to be determined at runtime, and so NSClassToString is useful. However, when using those classes from a static library it returns nil. I've had this issue before and solved it using the -ObjC linker flag.

However, for some reason in my current project this doesn't solve it. The only difference I can see is that in the first case (following a tutorial) I made an OSX static library, and changed the relevant parts to make it produce an ARMv6 version. In the second case I created a static library for iPhone OS, from the template.

It is indeed a puzzlement.


Edit

I found the solution, so here it is: I simply added the -ObjC linker flag to the target that used the static library as well as the target for the library itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消