• 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: creating instance using Class variable
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: creating instance using Class variable


  • Subject: Re: creating instance using Class variable
  • From: Ken Thomases <email@hidden>
  • Date: Wed, 20 Aug 2008 19:28:50 -0500

On Aug 20, 2008, at 7:02 PM, Eliza Block wrote:

Does anyone know how to use a variable of type Class to create an instance of an object?

I'm writing a method that needs to create an instance of one of two classes, to be determined at run-time. I want to pass in a Class variable, and have the method use it to create the instance. I know this must be possible, because NSMatrix does it. You can setCellClass: for NSMatrix, and then the cells it creates will be instances of the class you set. But I can't seem to find documentation about how to actually create the instance.

In short, I want to do something like this:
Class stringClass = [NSString class];
id myNewString = [[stringClass alloc] init];

But that doesn't work.

Doesn't work how?

I believe that's exactly how it's supposed to work.

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: creating instance using Class variable
      • From: Eliza Block <email@hidden>
References: 
 >creating instance using Class variable (From: Eliza Block <email@hidden>)

  • Prev by Date: Re: !foo vs foo == nil
  • Next by Date: Re: !foo vs foo == nil
  • Previous by thread: creating instance using Class variable
  • Next by thread: Re: creating instance using Class variable
  • Index(es):
    • Date
    • Thread