call a class from variable
How can I instantiate a class from a variable contents? For example:
I want to have classes called:
apple.com.h
apple.com.m
mac.com.h
mac.com.m
NSString *x="apple.com";
parseHTML = [[???? alloc] init];
How can I make an instance of apple.com.m without an if x="apple.com" statement? Like below:
parser = [[x alloc] init];
I believe the word is literal. I am trying to leave this 'open' so classes can be added as needed for different domains.
Thank you,
Todd
Hi Todd
This is an interesting question. I'm at work in California at the moment (on a Windows machine). I'll reply in more detail this evening. The basic answer however is that all objects are derived from a common base class (NSObject). You can call any method on any object because the run-time system looks up the method before dispatching to theobject.
Remember that Obj/C is C (and Obj/C++ is C++), so you can use a cast to keep the compiler quiet (however you can never fool the run-time system).
NSString* myString = (NSString*) [[NSObject alloc]init] ; When you call [myString capitalizedString] will fail because the run time system will ask myString if he has a method called 'capitalizedString' and he'll throw when he does not.
If you do:NSObject* notAString = [[NSObject alloc]init] ;
[notAString capitalizedString] ;
The compiler will complain (warn) that he isn't sure that notAString has a method called capitalizedString.
If you want to defer the allocation of objects totally until runtime, something like this:
NSString* today = @'Monday' ; // something you know at run time
NSObject myObject;
// the following syntax is wrong !!!!
myObject = [[today alloc]init] ; // create an object at run-time
I know I don't have the correct syntax for this - however I'm confident that this is possible.
I hope this sets you off in the right direction. Please let me know if I'm not answering the question that is in your mind!
Hi Robin,
I think you are answering my question. In your example, for example, you are trying to create an object from the 'Monday.m' class, correct? Again in your example, then later in the code I could call a method and it would use 'monday.m or tuesday.m or wednesday.m', according to today.. This simple example seems like a harder way of doing it until say, a new day was added to the week, then a new class with the same methods could be created and the existing code should still work on it, like a plug in.
I am sure I did this with PHP and it had something to do with literal, as in using the variable literally. But it escapes me now.
I may be thinking inside my own little box here also. This could very well be a BAD approach to my issue. I am trying to write "good" code and not stray too far from accepted practices. Please let me know if this is the wrong approach.
Thank you,
Todd
Thank you very much for the response. That is exactly what I was looking for. After you told me what I was looking for I did a bit of research myself and found that term. The context in which I found it referenced was for making plug-ins, it all makes sense now! For the record here is a pretty good tutorial of plug-in architecture:
http://www.cocoacast.com/?q=node/175. Thank you for pointing me in the correct direction....I now have a lot more research to do.
Todd
So I added a class and called it using:
id theLetter = [[NSClassFromString(chosenLetter) alloc] init];
All is good, and my dynamic class is loaded correctly, and methods from that class can be called. However, I needed to add empty methods to my controller to avoid compiler warnings. Warnings that the methods did not exist. Not really a big deal. But I was browsing the documentation and the recommended way to instantiate it is:
Class theLetter = [[NSClassFromString(chosenLetter) alloc] init];
But here the empty methods do not passify the compiler.....and I get method missing warnings at compile.
Both of the above lines works correctly, just get compiler warnings.
Any ideas?
Todd
Hi Todd
Thanks for your thanks. And for the link. Interesting.
Yes, Obj/C++ is really good stuff. It's a software gem. I've always thought it's kind of difficult to approach and learn. However I suspect that's just me. Too much C++ destroys the brain cells.
Anyway, the warnings from the compiler are well intended paranoia.
Several possibilities spring instantly to mind:
1) There's a command-line option to the compiler to say "stop being an old woman"
2) Perhaps there's a pragma to calm his fears
3) You can use a C cast [(MyClass*) theLetter doThis: data] ;
4) Cast it at birth MyClass* myClass = (MyClass*) [[NSClassFromString ....] ;
I don't know if there is compiler option to silence him. However I think you should avoid that because those warnings are very helpful for the detecting typos and stuff. They are really useful. I suspect option (4)'s the winner. I look forward to hearing what you find.
I do not want to quiet him altogether. Just the dynamic class warning. I suspect it is because the compiler has noway of knowing what class it is going to be so it can not find the method declarations for said class.
Todd