[Newbies] Exporting Object Memory

David T. Lewis lewis at mail.msen.com
Tue Dec 18 00:02:17 UTC 2007

On Mon, Dec 17, 2007 at 10:57:05AM -0500, Chris Cunnington wrote:
> If I had data in object memory, because I've saved answers from a user into
> objects, then can I file out that information? If I had to send it to
> somebody as an csv file, how could I do that easily?
> I know if I could find an object with the inspector I could print data in
> the Transcript and cut and paste it into another file, but is there another
> way? 
> It seems to me that filing out gives you the application, but what if I
> wanted information that had been entered by the users, the end user data?


Take a look at class Object, and the methods in method category "printing".
In particular, look at the methods #printOn:, #printString, #storeOn:, and
#storeString. These are the basic methods used for converting objects
into human-readable or computer-readable strings (or for writing them
onto streams, such as file streams, in the case of #printOn: and #storeOn:).
They are overridden by many classes in the image, so the methods that
you see in class Object are the defaults. 

One simple and useful way to convert objects into CSV is just to make
up your own methods called #printCsvOn: and #printCsvString (or something
like that). Write the code to spit out the fields in your objects with
commas in between, and viola you have a CSV file writer. This is fairly
easy to do, and it makes a handy way to dump your data to a file that
can be imported by a spreadsheet. If your data might include comma characters,
you can try writing it out in tab-separated format (i.e. #printTsvOn:)
instead of comma-separated fields.

It's not fancy but it works fine.


More information about the Beginners mailing list