• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Using Categories
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Using Categories
      • From: Michael Latta <email@hidden>
    • Re: Using Categories
      • From: Ryan Bates <email@hidden>
  • Prev by Date: Re: Tabs window autoresizing.
  • Next by Date: Re: catching uncaught exceptions
  • Previous by thread: Re: Localizable.strings *just stop working*
  • Next by thread: Re: Using Categories
  • Index(es):
    • Date
    • Thread