Making init return a subclass of the Class being init-ed & circular imports
Making init return a subclass of the Class being init-ed & circular imports
- Subject: Making init return a subclass of the Class being init-ed & circular imports
- From: Diggory Laycock <email@hidden>
- Date: Tue, 29 Jul 2003 01:55:40 +0100
Hi,
I am in the process of writing a set of Classes that represents NMEA
Sentences (The language that GPS devices speak.)
These sentences conform to a general pattern and I have written a
generic class that can initialise itself by parsing the incoming NMEA
data. (MFNMEASentence)
There are several different standard NMEA sentence-types (containing
different kinds of data. - e.g. some describe the Number of Satellites
acquired, others describe Geographic Position etc.. )
My plan to is to have subclasses of this standard sentence object. I
can then add methods to these subclasses that are relevant to the
content of the sentence-type.
So far so good - but what I really want to do is to get the Parent
Generic Sentence Class (MFNMEASentence) to be able to return a specific
subclass of itself depending on the sentence-type it is initialised
with. (falling back to returning the generic sentence object if there
is no appropriate subclass )
I'm afraid I'm not entirely sure of the best way of doing this - Here
is my best shot:
so far I have something a bit like the code below in MFNMEASentence:
I have a couple of questions -
1 - Is this a good idea? Should I be doing it like this?
2 - The code doesn't actually compile - it's got some kind of circular
import going on
- MFNMEASentence.h (generic superclass) has to import
MFNMEARMCSentence.h (the subclass) because it uses it when it init's
the subclass.
- but MFNMEARMCSentence.h has to import MFNMEASentence because it is a
subclass of it.
so I get the following error when I try to compile:
MFNMEARMCSentence.h:37: cannot find interface declaration for
`MFNMEASentence', superclass of `MFNMEARMCSentence'
Thanks for any help -
Diggory Laycock.
code:
-(id)initWithString: (NSString*)initString;
{
if (self = [super init])
{
sentenceString = [[NSString init] alloc];
fieldsArray = [[NSMutableArray alloc] init];
[self setSentenceString: initString];
if ([self decodeSentence])
{
// We scanned a properly formed NMEA sentence into our inst Vars
properly.
// Check to see what kind of sentence it is and init and return
// the appropriate subclass instead
// Only the top-level generic object should do this - we don't want
subclasses to inherit this part of initWithString:
if ([self isMemberOfClass: [MFNMEASentence class] ] )
{
if ([[self sentenceIDString] isEqualToString: @"RMC"])
{
// NSLog (@"Recommended Minimum
Specific GPS/Transit Data");
MFNMEARMCSentence* rmc =
[[MFNMEARMCSentence alloc] initWithString: sentenceString];
[self autorelease];
return rmc;
}
if ([[self sentenceIDString] isEqualToString: @"VTG"])
{
// etc.....
}
// Check for other types of sentences here...
}
return self;
}
}
return nil;
}
Diggory Laycock
-----------------------
http://www.monkeyfood.com
_______________________________________________
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.