Unless you're explicitly loading in bundles or plugins, I wouldn't think you need to force the class to be loaded. I just tested a call to NSClassFromString(nameOfTestClass) as the first line of my main() function and it worked.

In situations like this I try to question my most petty assumptions.

Suppose one of the classes in question is named SomeClass. I would add a couple of lines like this to make sure the string is what you think it is and that SomeClass has actually been compiled into your application (maybe you accidentally removed it from the target in Xcode?):

    NSLog(@"clsName is [...@]", clsName);
    NSLog(@"the [...@] class is definitely loaded", [SomeClass class]);

When I NSLog, I often put square brackets around strings in case there are spaces or newlines that I might not notice.

--Andy

On Jan 15, 2009, at 1:27 PM, Mohan Parthasarathy wrote:

Hi,

I have tried this to make sure that the class is loaded, but not working:

[[NSBundle mainBundle] classNamed:clsName];
[self loadBundleForClass:clsName];
Class cls = NSClassFromString(clsName);

I still get nil.. What am i missing ?

thanks
mohan

On Thu, Jan 15, 2009 at 9:17 AM, Mohan Parthasarathy <surut...@gmail.com >wrote:

Both of the methods return nil and as per the document it says "Class is not loaded". Is there a compile time option to load the classes or only way
to do it as at runtime..

thanks
mohan



On Wed, Jan 14, 2009 at 6:46 AM, Adam Venturella <aventure...@gmail.com >wrote:

The output is the same, but there is also:
#import <objc/runtime.h>
objc_getClass([myString UTF8String]);

I am going to guess that NSClassFromString(myString) is probably using
objc_getClass(), maybe not.  I didn't know NSClassFromString existed
though, so I am switching to that instead of using the runtime.h
function.




On Tue, Jan 13, 2009 at 8:45 PM, Mohan Parthasarathy <surut...@gmail.com >
wrote:
On Tue, Jan 13, 2009 at 8:06 PM, Ken Thomases <k...@codeweavers.com>
wrote:

On Jan 13, 2009, at 3:33 PM, Jean-Daniel Dupas wrote:

he is just talking about class name, not class.

NSClassFromString() is probably what you're looking for.

NSMutableString *clsName = derive class name from the entry.

Class cls = NSClassFromString(clsName);

id<YouProtocol> instance = [[cls alloc] init];


I'll just add the following:

If you don't need such complete flexibility -- for example, if you're selecting from a fixed set of classes by some tag -- then you don't
need to
compute a class name and look up the class that way.

Classes are objects and so they can be stored in collections.  For
example,
you could have a lookup dictionary that mapped from keys to class
objects.
You would construct the dictionary like this:

      [NSDictionary dictionaryWithObjectsAndKeys:
              [SomeClass class], "key1",
              [OtherClass class], "key2",
              [ThirdClass class], "key3",
              // ... etc.
              nil];

This adds a small amount of safety in the same way that
statically-specified stuff generally does. For example, the compiler
will
catch typos in class names.


Thanks for this suggestion. This looks fairly clean except that the
space is
allocated at the beginning. This may not be a big deal in some cases. In
the
other way, you allocate space and insert in the dictionary only when
needed.
Also, eventually i need the real object instances inserted in the
dictionary.

-mohan


-mohan



Cheers,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

http://lists.apple.com/mailman/options/cocoa-dev/aventurella%40gmail.com

This email sent to aventure...@gmail.com




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/aglee%40mac.com

This email sent to ag...@mac.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to