• 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: Constructing class names dynamically
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Constructing class names dynamically


  • Subject: Re: Constructing class names dynamically
  • From: Ken Thomases <email@hidden>
  • Date: Tue, 13 Jan 2009 22:06:40 -0600

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.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Constructing class names dynamically
      • From: "Mohan Parthasarathy" <email@hidden>
References: 
 >Constructing class names dynamically (From: "Mohan Parthasarathy" <email@hidden>)
 >Re: Constructing class names dynamically (From: Bill Bumgarner <email@hidden>)
 >Re: Constructing class names dynamically (From: Jean-Daniel Dupas <email@hidden>)

  • Prev by Date: Re: runModalForWindow, best solution to modal session
  • Next by Date: Re: Constructing class names dynamically
  • Previous by thread: Re: Constructing class names dynamically
  • Next by thread: Re: Constructing class names dynamically
  • Index(es):
    • Date
    • Thread