• 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: substitute class at runtime?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: substitute class at runtime?


  • Subject: Re: substitute class at runtime?
  • From: Andy Lee <email@hidden>
  • Date: Sat, 19 Jul 2008 09:54:04 -0400

On Jul 19, 2008, at 9:27 AM, Uli Kusterer wrote:
On 19.07.2008, at 13:03, Jean-Daniel Dupas wrote:
Because "one" don't now that "Class" are just NSObject and can be use like any other objects.


Yeah, if the sample code really is like it's actually implemented, and it wasn't simplified for posting, just placing the result of [NSlass class] into an array would be a better idea.

That said, I've implemented plugin schemes where plugin authors put the class names as strings in plist files and then we used NSClassFromString() to get the class the plugin developer requested. That's effectively what NSApplication and NSBundle do when you ask for the application class/main class to use.

I can't speak for Nathaniel -- maybe an array of classes is indeed the right solution -- but it's not so hard to imagine a situation where you have class names that you need to convert to classes. Uli just gave an example. I do it in my own app (I have a configuration plist that contains class names). There's also a little thing called Interface Builder that stores class names in xib files.


Just to clarify a couple of earlier replies: if the array had contained class objects in the first place (using [ClassOne class], [ClassTwo class], etc.), you wouldn't need to send a +class message in the ??????? part of the code; you can just use the class object directly.

Class myClass = [NSString class];
NSObject *s = [[myClass alloc] init]; // don't have to say [myClass class]


--Andy



Of course, stuff like that is a tad dangerous if you have to fear some other people feeding you malicious class names, so don't use NSClassFromString([NSString stringWithFormat: @"NS %@Element",currentHTMLTagName]) to implement your web browser unless you want to cope with people creating NSXMLElements for you to get your browser to crash... :-)

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________

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


References: 
 >substitute class at runtime? (From: Nathaniel Gottlieb-Graham <email@hidden>)
 >Re: substitute class at runtime? (From: Andy Lee <email@hidden>)
 >Re: substitute class at runtime? (From: "Kyle Sluder" <email@hidden>)
 >Re: substitute class at runtime? (From: Andy Lee <email@hidden>)
 >Re: substitute class at runtime? (From: "Kyle Sluder" <email@hidden>)
 >Re: substitute class at runtime? (From: Jean-Daniel Dupas <email@hidden>)
 >Re: substitute class at runtime? (From: Uli Kusterer <email@hidden>)

  • Prev by Date: Re: Dot Syntax docs missing?
  • Next by Date: How to customize View to combine UITextView and UITextField?
  • Previous by thread: Re: substitute class at runtime?
  • Next by thread: Re: substitute class at runtime?
  • Index(es):
    • Date
    • Thread