Re: Using Categories
Re: Using Categories
- Subject: Re: Using Categories
- From: Michael Latta <email@hidden>
- Date: Fri, 13 Feb 2004 20:14:48 -0800
Categories affect the definition of the class. Methods are added to
the dispatch table of the class to be specific. Since ObjectiveC does
not care if an object is known to implement a method at compile time,
this works. At run-time the class implements the methods. The linker
in particular does the patching, it is not a run-time change to the
class. All instances of a class implement the methods of all
categories linked into the executable. The only caveat is ZeroLink.
There has been some weird behavior with that one since the linking is
being done on-demand.
Michael
On Feb 9, 2004, at 11:12 AM, Alexander F. Hartner wrote:
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.
_______________________________________________
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.