Re: Newbie question on saving data
Re: Newbie question on saving data
- Subject: Re: Newbie question on saving data
- From: Philip Riggs <email@hidden>
- Date: Mon, 8 Nov 2004 22:16:20 -0700
On Nov 8, 2004, at 9:35 PM, email@hidden wrote:
I was sort of hoping that there was some easier save and load method
that I could use that would take my array and just save it to a text
file in ASCII or something that I could just as easily import to excel.
And then import back into my Cocoa app.
Because I'm so new to programming (and just learning it as a hobby) I'm
probably not explaining myself well enough. But I was really hoping I
could save data in the same "look at feel" the data shows up in my
TableView.
But when I have data like this:
Name Age
John Doe 24
Bill Jones 33
I want to be able to somehow get it saved like that (i.e., human
readable format).
I'm sure this isn't the best method, but it was simple and always worked when I first started learning Cocoa and needed a quick method to export/import data between Excel and some code I was using.
Anytime I need Excel to read something, I export it as .csv format. It always works (but don't trust Excel's .csv export, it frequently does strange things to data with <null> cells). Plus, .csv works with other programs, such as ChartSmith. I don't know how you are keeping your data, but if each line of your data is composed of NSStrings and NSNumbers stored in an array use the NSArray method to store in an intermediary NSArray:
<x-tad-bigger>- (</x-tad-bigger><x-tad-bigger>NSString</x-tad-bigger><x-tad-bigger> *)componentsJoinedByString:(</x-tad-bigger><x-tad-bigger>NSString</x-tad-bigger><x-tad-bigger> *)</x-tad-bigger><x-tad-bigger>separator</x-tad-bigger>
with the separator being @",". After each line is an element in an array, use the same method on the intermediary array with @"\n" as the separator. Then save the NSString using
<x-tad-bigger>- (BOOL)writeToFile:(NSString *)</x-tad-bigger><x-tad-bigger>path</x-tad-bigger><x-tad-bigger> atomically:(BOOL)</x-tad-bigger><x-tad-bigger>flag</x-tad-bigger>
To read it back,
<x-tad-bigger>+ (id)stringWithContentsOfFile:(NSString *)</x-tad-bigger><x-tad-bigger>path
</x-tad-bigger>
Then following the previous procedure in reverse:
<x-tad-bigger>- (</x-tad-bigger><x-tad-bigger>NSArray</x-tad-bigger><x-tad-bigger> *)componentsSeparatedByString:(NSString *)</x-tad-bigger><x-tad-bigger>separator</x-tad-bigger>
Philip Riggs
_______________________________________________
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