views:

46

answers:

2

I'm making a class that initializes instances of certain classes. This class will be used to initialize a few different types of classes all subclassed from a common super class. Currently I am using an instance variable:

Class templateClass;

to store the class type. I get compiler warnings saying methods aren't supported by templateClass. Is there a way to suppress these warnings or specify that the template class must be a subclass of a certain class?
Also, is this the ideal way to go about this in objective-c? Is there a different way to specify the class to use that I should use?
Thanks for the help!

Edit: The class I am writing is designed to create sprites in randomly chosen locations with a few variables to limit them. This class expects the class it rezzes to have an initWithX:(int)pos yVariance:(int)variance method. I expect this can be defined using the protocals you mentioned. I will be making multiple instances of the rezzing class for each type of sprite I must create. I want to be able to send the rezzing class initWithTemplateClass:(Class)templateClass to define the type of sprite to create. I'm not sure if this is the correct way to go about this though because the compiler posts that the method initWithX:(int)pos yVariance:(int)variance is not available for the templateClass. I expect there is a problem in the way I am going about this. The code that is showing the warning is:

[self addChild:[[templateClass alloc] initWithX:positionOfChild  
  yVariance:(random()%(rowVarianceSize+1))]];
+4  A: 

You can define a protocol with all required methods in it. Then declare your classes conform to that protocol.

Vladimir
I need to let the class that takes care of the initializing know which type to initialize though. Is there a better way to do that then storing it using the `Class` type?
dhatch387
I'm making a game and one class takes care of rezzing sprites. I want to use it to rez multiple types of sprites but I'm not sure if using the Class type is the correct way to define which type of sprite it should rez. The sprites are all different subclasses of one superclass.
dhatch387
Sorry, it seems my answer is not exactly what you need... Could you give more info on how do you want to create you sprites, how you pass the sprites type...
Vladimir
Just edited the post with some info. Let me know if you need more info.
dhatch387
I found a question relating to this one referencing use of protocols. I'm just not sure if there is a fundamental design issue with the way I'm doing things. Its at: http://stackoverflow.com/questions/3278442/iphone-how-to-specify-the-class-data-type-have-to-adopt-to-a-protocol
dhatch387
+1  A: 

Edited to add a 2nd alternative

I don't get the that warning (nor did I expect to) when I compile a simple test case that attempts to do what you're doing.

That said, what you're doing seems perfectly reasonable. Though you do have a leak in the 'addChild' example. You alloc/init your new object which leaves it with a retain count of 1, then (assuming 'self' is a UIView) your call to addChild will also retain the object, but you have no reference of your own to release.

id obj = [[templateClass alloc] initWithX:positionOfChild yVariance:(random()%(rowVarianceSize+1))]];
[self addChild:obj];
[obj release], obj = nil;

Also, Vladimir's suggestion to define a protocol is a good one, and if you do so, you can even insert a runtime check that the templateClass you were given conforms to that protocol.

if ([templateClass conformsToProtocol:@protocol(Foo)]) {
    id<Foo> obj = [[templateClass alloc] initWithX:positionOfChild yVariance:(random()%(rowVarianceSize+1))]];
    [self addChild:obj];
    [obj release], obj = nil;
} else {
    // Do whatever you want to do if templateClass isn't correct.
    // Obviously you could also have the protocol conformance check elsewhere.
}

However, as an alternate to all of that, instead of using your Class instance variable, you could define an interface for a factory object that creates the particular type of sprite you want. Then pass in an appropriate factory class instance.

For example:

@interface Sprite : UIView {
}
- (id)initWithX:(int)pos yVariance:(int)variance;
@end

@interface SpriteFactory {
}
- (Sprite*)createSprite;
@end

Then create pairs of subclasses: a Sprite subclass, and a SpriteFactory subclass which creates that kind of sprite.

Then instead of using your 'Class templateClass' you would have a 'SpriteFactory* factory' instead.

imaginaryboy
thanks very much!
dhatch387