Can I specify a specific superclass requirement for a class type variable?
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))]];
You can define a protocol with all required methods in it. Then declare your classes conform to that protocol.
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.
精彩评论