• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Loading classes for use with NSClassFromString, how?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loading classes for use with NSClassFromString, how?


  • Subject: Re: Loading classes for use with NSClassFromString, how?
  • From: Nicko van Someren <email@hidden>
  • Date: Mon, 16 May 2005 15:13:36 +0100

On 16 May 2005, at 14:55, Ken Tozier wrote:

On May 16, 2005, at 9:45 AM, Nicko van Someren wrote:

On 16 May 2005, at 14:37, Ken Tozier wrote:

I need to convert some stored strings to classes so that I can run one of their methods. I thought I could use NSClassFromString() like so
...
But apparently the class isn't loaded yet as NSClassFromString() always returns nil. Is there a way to load a specific class at runtime?

Assuming that you are getting the code that implements the necessary code from some bundle you can use the NSBundle method -(Class)classNamed:(NSString *)className This will load the bundle's executable code if it is not already loaded.

The classes in question are all in the main bundle (as far as I know as they weren't placed into a separate bundle unless Xcode did it behind the scenes) so they should be loaded when the app launches, correct? If so, is there another way to create an instance of a class when all you have is it's name in string form?

I'm guessing here, but it's possible that the code for your class is not getting loaded at startup if that class is not linked to the mail executable by default (e.g. nothing in your main program explicitly references it in Objective-C code). You could try:
[[NSBundle mainBundle] classNamed: @"MyClassName]
If that does not work then I suspect that something in the build process is causing the code for your class not to end up in the executable (like perhaps it has become unticked in the Target column in Xcode for the particular build you are making).


	Nicko

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Loading classes for use with NSClassFromString, how? (From: Ken Tozier <email@hidden>)
 >Re: Loading classes for use with NSClassFromString, how? (From: Nicko van Someren <email@hidden>)
 >Re: Loading classes for use with NSClassFromString, how? (From: Ken Tozier <email@hidden>)

  • Prev by Date: Re: embedding web server in cocoa app
  • Next by Date: Re: OpenGL and Tiger
  • Previous by thread: Re: Loading classes for use with NSClassFromString, how?
  • Next by thread: NSSocketPort question: retainCount
  • Index(es):
    • Date
    • Thread