Category vs Subclass
Category vs Subclass
- Subject: Category vs Subclass
- From: Christian Mike <email@hidden>
- Date: Thu, 16 Aug 2001 11:11:40 -0500
I don't recall seeing this discussed here before, but I could have missed
it.
When is it appropriate to use a Category versus a subclass? For instance, I
attempted:
@interface ScannerAdditions: NSScanner
{}
- (BOOL)scanLong:(long *)longValue;
@end
@implementation ScannerAdditions
- (BOOL)scanLong:(long *)longValue
{
int iValue;
BOOL bResult;
bResult = [self scanInt:&iValue];
*longValue = (long) iValue;
return bResult;
}
@end
Now in my code, I did:
long lValue;
ScannerAdditions *myScanner = [ScannerAdditions
scannerWithString:@"123,something else"];
[myScanner scanLong:&lValue];
This throws the exception: [NSConcreteScanner scanLong:]: selector not
recognized
Now, if I change the above subclass into a category:
@interface NSScanner (ScannerAdditions)
-and-
@implementation NSScanner (ScannerAdditions)
-and-
NSScanner *myScanner = [NSScanner scannerWithString:@"123,something
else"];
Then the above code works fine.
So this leads to my question:
When is it appropriate to use a Category versus a subclass?
Thanks.
Michael Christian
Thomson multimedia Inc.