RE: Convenience Methods
RE: Convenience Methods
- Subject: RE: Convenience Methods
- From: Jeff Laing <email@hidden>
- Date: Thu, 27 Sep 2007 09:37:48 +1000
> Sometimes when I'm writing convenience constructors I might write
>
> + (MyClass *)myClassWithFoo:(int)f
> {
> return [[[self alloc] initWithFoo:f] autorelease];
> }
>
> where "self" is, of course, the class. A good reason to do that is
> that it means that a subclass only has to override -initWithFoo: in
> order to make +myClassWithFoo: work as expected.
I'm still undecided on whether this is a good thing or not. I'm going to go
one level more specific.
@class Animal
+ (Animal*)animalWithName:(NSString*)n { ... }
- (Animal*)initWithName:(NSString*)n { ... }
@end
@class Dog(Animal)
@end
Now, in this case, you can call
Dog *pet = [Dog animalWithName:@"Rover"];
but that feel uncomfortable to me. I'd still be inclined to add another
method so I could write:
Dog *pet = [Dog dogWithName:@"Rover"];
even if all it did was wrap up the same methods. ie,
+ (Dog*)dogWithName:(NSString*)n
{
return [[[[self class] alloc] initWithName:n] autorelease];
}
Its clearly a subjective issue, a comfort thing. At the very least, it
gives me somewhere different to put a breakpoint when it all goes "to the
dogs", so to speak.
_______________________________________________
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