Re: Instance an from an NSString Class name
Re: Instance an from an NSString Class name
- Subject: Re: Instance an from an NSString Class name
- From: email@hidden
- Date: Tue, 21 Sep 2010 22:21:34 -0600
Thanks! Now I have:
NSString *class = [NSString stringWithCString:[data bytes]
encoding:NSASCIIStringEncoding];
Class obstacle = NSClassFromString(class);
id thisObstacle = [[obstacle alloc] initWithImage:[sender
draggedImage] andLocation:[sender draggingLocation]];
[m_obstacles addObject:thisObstacle];
And I now have an instance of the class stored in an array. I don't
think I have to retain thisObstacle as m_obstacles take ownership.
However, must I retain [sender draggedImage] so the code becomes:
NSString *class = [NSString stringWithCString:[data bytes]
encoding:NSASCIIStringEncoding];
Class obstacle = NSClassFromString(class);
id thisObstacle = [[obstacle alloc] initWithImage:[[sender
draggedImage] retain] andLocation:[sender draggingLocation]];
[m_obstacles addObject:thisObstacle];
On Sep 21, 2010, at 10:08 PM, Stephen J. Butler wrote:
On Tue, Sep 21, 2010 at 11:01 PM, <email@hidden> wrote:
Given an NSString which is the name of a class what voodoo do I
perform to
create an instance of that class?
Use NSClassFromString().
_______________________________________________
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