6138 Views 5 Replies Latest reply: Mar 14, 2006 9:22 AM by Daniel Stein
Even if you use the C library functions referred to above, you may eventually have to encapsulate the data in Cocoa objects in order to plot them in a GUI. If you go that way, some of what follows may help you with that as well.
If you are using Cocoa, as indicated by your reference to NSData, let me advise you to try NSString methods.
First of all, since you have an ASCII file, you can import the entire contents with the NSString class method stringWithContentsOfFile:
The argument for this method is another NSString representing the file system path to the file. NSString has lots of good methods for dealing with file system paths.
Once you have the data represented as an NSString object, you can use other NSString methods to parse the file contents into an array of individual NSString objects using such methods as componentsSeparatedByString which allow you to specify a separator character such as <tab> or some other character as the separator.
I hope this helps you get started.
Certainly NSData is a way to go, but NSString methods are very powerful. Investigate the properties of this important Cocoa Foundation framework class.
Here's the interface for a very simple graphical object:
@interface PlotPoint: NSObject
- ( id ) initWithRect: ( NSRect ) aRect;
- ( void ) setColor: ( NSColor * ) aColor;
- ( NSColor * ) color;
- ( NSRect ) rect;
- ( void ) plot;
The class has an initializer, some accessors, and a plot method for actually drawing the object on-screen. You could start with something as simple as:
- ( void ) plot
[ color set ];
NSRectFill( rect );
//NSBezierPath *path = [ NSBezierPath bezierPathWithOvalInRect: rect ];
//[ path stroke ];
The NSBezierPath is just a suggestion in case you want to make a more complicated symbol. Just assemble the pieces of a path and draw it. Or if you make a closed-path, you could [ path fill ] it. If you go that way, you will probably want to make one of the instance variables "origin" instead of "rect".
In the above example, the rect ivar contains the location on screen and the size for the plot point object. That is the information you get from your ASCII file, which you would use when initializing the PlotPoint object.
The basic idea is to make a list of objects you want to draw, say, in a mutable array, and then in the drawRect method for your custom view, enumerate the list and have each object draw itself
while ( obj = [ enumerator nextObject ] ) [ obj plot ];
If the object is a PlotPoint object (or even a subclass of it!), this should go off without a hitch. Good luck with your plotting program!