Determining the 'primitive' methods of a class
Determining the 'primitive' methods of a class
- Subject: Determining the 'primitive' methods of a class
- From: Matt Gough <email@hidden>
- Date: Tue, 14 Oct 2003 16:27:00 +0100
I took it on myself to subclass NSScanner. Before I did so, I had no
understanding of the pitfalls of such a path. After a few hours discovering
about the joys of class clusters and how to subclass such classes I managed
to build and run my app with the following subclass:
@interface MyScanner : NSScanner
{
NSScanner* _embedded;
}
-(MyScanner*) initWithString:(NSString*) string;
// Primitive methods we must override as NSScanner is a cluster
- (NSString *)string;
- (unsigned)scanLocation;
- (void)setScanLocation:(unsigned)pos;
- (void)setCharactersToBeSkipped:(NSCharacterSet *)set;
- (void)setCaseSensitive:(BOOL)flag;
- (void)setLocale:(NSDictionary *)dict;
/* Lots of my own methods not listed here for simplicity */
@end
@implementation MyScanner
-(MyScanner*) initWithString:(NSString*) string
{
_embedded = [[NSScanner alloc] initWithString:string];
if (_embedded)
[_embedded setCharactersToBeSkipped:
[NSCharacterSet characterSetWithCharactersInString:@""]];
return self;
}
- (NSString *)string
{return [_embedded string];}
- (unsigned)scanLocation
{return [_embedded scanLocation];}
- (void)setScanLocation:(unsigned)pos
{[_embedded setScanLocation:pos];}
- (void)setCharactersToBeSkipped:(NSCharacterSet *)set
{[_embedded setCharactersToBeSkipped:set];}
- (void)setCaseSensitive:(BOOL)flag
{[_embedded setCaseSensitive:flag];}
- (void)setLocale:(NSDictionary *)dict
{[_embedded setLocale:dict];}
@end
This worked fairly well, except for the fact that the empty character set
that I used for setCharactersToBeSkipped was not being honored, whereas it
does when just using a non-subclassed NSScanner.
I was able to fix this by also adding an implementation of:
- (NSCharacterSet *)charactersToBeSkipped;
(I also added caseSensitive and locale for good measure)
Now the crux of the issue;
My interpretation of the section on primitives in:
<
http://developer.apple.com/documentation/Cocoa/Conceptual/Foundation/Concep
ts/ClassClusters.html>
was that the primitive methods are the ones in the non-categorized interface
to a class. This is why my initial attempt had setCharactersToBeSkipped, but
not charactersToBeSkipped as that is in NSScanner (NSExtendedScanner).
(In hindsight it seems obvious that a 'setSomething' primitive method also
needs an equivalent 'something' primitive for the base class to get to it.)
So my questions are:
1. Is my understanding of what constitutes a primitive wrong, or is
NSScanner 'wrong'?
2. What happens if other primitive methods get added to NSScanner in future
that my existing code knows nothing about? Will my app fail if NSScanner
uses such a primitive on an object which is really a MyScanner?
I realise that I could do what I am doing by using a category instead of a
subclass. However since MyScanner is only for use with a very specific
string format it made more sense for it to be a subclass.
Thanks for any guidance
Matt Gough
_______________________________________________
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.