Re: how to build data parser
Re: how to build data parser
- Subject: Re: how to build data parser
- From: Pandaa <email@hidden>
- Date: Sun, 27 Mar 2005 08:27:13 +0200
2005-03-27 kl. 05.27 skrev Daniel Child:
Hi All,
In my short time doing Objective-C, I've had to parse data files on several occasions and want to build a more generic parser. I'm thinking along these lines.
+ (DataParser *)parserWithFieldDelim:(NSString *)fd andRecordDelim:(NSString *)rd;
- (NSMutableArray *)parseFileAtPath:(NSString *)path withObjectType:(NSObject *)ot;
The parse file method would return an array of objects. I'm stuck with two things I haven't done before. How do I specify an object type (e.g. Word class or Stroke class object)?
By a Class object? i.e. [Word class] or [Stroke class]
And once I have scanned individual lines of text, how can assign the parsed fields to the correct member variable fields of the object class being passed in?
It may be a bad idea to directly assign the parsed fields to member instance variable fields as this would break encapsulation. Unless the classes in question are very simple, only the alloc/init methods of the class would know how to properly init an object of the type and there is no way to determine from the outside how to match data read from the file with instance variables without additional knowledge of the class.
I think I would want to be able to specify the number of expected fields and the field types (string or number), but am not sure how an ordering of fields is accomplished. Thinking back to my days with Fourth Dimension, I'm looking for a way to convey that:
field 1 is a string
field 2 is a number
such that if, for example, a Word class, has
NSString *word
NSNumber *frequency
as instance variables, there will be a way to init members of the class properly.
The way to init an object of the Word class is to call the proper alloc and init methods. Having your data parser create and init objects itself may lead to potential bugs with nontrivial classes. For example, there will be no way for your data parser to know the size of variable-sized objects so these may be allocated too little space by the data parser.
Instead of dealing with instance member fields directly, have you considered using something like factory methods?
Any ideas how to go about doing this?
That said, if you really want to do this, see, e.g. :
http://www.cocoabuilder.com/archive/message/cocoa/2002/5/1/12074
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. email@hidden . . www.synapticpulse.net .
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden