• 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
Category vs Subclass
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


  • Prev by Date: Re: void value not ignored as it ought to be??
  • Next by Date: Re: Online C++ resources?
  • Previous by thread: Re: void value not ignored as it ought to be??
  • Next by thread: Re: Category vs Subclass
  • Index(es):
    • Date
    • Thread