Using Categories
Using Categories
- Subject: Using Categories
- From: "Alexander F. Hartner" <email@hidden>
- Date: Mon, 9 Feb 2004 21:12:11 +0200
Let say I want to add a category to an existing class (CAR). Car.m
imports Car.h. SuperDuperCar.h imports Car.h and SuperDuperCar.m
imports SuperDuperCar.h.
My question is:
For me to make use of the additional functionality added within the
category, do i need to import Car.h or SuperDuperCar.h ?
Once SuperDuperCar.h is loaded / imported, are other instanced of Car
instantited before the loading of the category also transformed to
SuperDuperCar's ?
Thanks
Alex
Car.h Header File
#import <Foundation/Foundation.h>
#import "Vehicle.h"
@interface Car : Vehicle
{
@private
int numberOfPassengers;
}
+ alloc;
- initWithValues:(NSString*)aName model:(NSString*)aModel
topSpeed:(int)aTopSpeed numberOfPassengers:(int)aNumberOfPassengers;
-(NSString*)description;
@end
Car.m Implementation File
#import "Car.h"
@implementation Car
+ alloc
{
NSLog(@"Allocating a CAR");
return [super alloc];
}
-(void)dealloc
{
NSLog(@"De-allocating a CAR");
[super dealloc];
}
- initWithValues:(NSString*)aName model:(NSString*)aModel
topSpeed:(int)aTopSpeed numberOfPassengers:(int)aNumberOfPassengers
{
NSLog(@"Initialising values on a CAR");
[super initWithValues:aName model:aModel topSpeed:aTopSpeed];
numberOfPassengers=aNumberOfPassengers;
return self;
}
-(NSString*)description
{
NSMutableString *description = [[NSMutableString alloc]init];
[description appendString:@"CAR : "];
[description appendString:[super description]];
[description appendString:@" Number of passengers : "];
[description appendString:[[NSNumber numberWithInt:numberOfPassengers]
stringValue]];
return [description autorelease];
}
@end
SuperDuperCar.h Header File for Category
#import "Car.h"
@interface Car ( SuperDuperCar )
-(void)drive;
@end
SuperDuperCar.m Implementation File for Category
#import "SuperDuperCar.h"
@implementation Car ( SuperDuperCar )
-(void)drive
{
NSLog(@"Driving the following CAR as specified in SuperDuperCar");
NSLog([self description]);
NSLog(@"Finnished driving CAR");
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.