RE: Files and arrays
RE: Files and arrays
- Subject: RE: Files and arrays
- From: Jeff Knee <email@hidden>
- Date: Thu, 14 Nov 2002 23:46:42 -0500
Mike Brink wrote:
> Sorry if this is a basic question, but I don't know where to even look
> for the answer. I'm sure I'll get the standard "RTFM", but I've
checked
> out NSFileWrapper class, but didn't see the info I was looking for.
Here is a quick snippet of code I snagged from a little utility I wrote
a while ago and modified for you that might get you going down the
right track... this is only one way to do it and it probably meets your
requirements.
NSString *linesFromFile = [[NSString alloc]
initWithContentsOfFile:@"~/addr.txt"];
NSArray *lineArray = [linesFromFile
componentsSeparatedByString:@"\n"];
NSString *oneLine;
int lineCount = 0;
NSEnumerator *lineEnumerator = [lineArray objectEnumerator];
while (oneLine = [lineEnumerator nextObject])
{
// you'll do more here... create an instance of Person
// grab data from the line and stick it in that instance
// add the instance to another NSArray...
lineCount++;
}
Inside the while loop you could create another NSArray using the
NSString componentsSeparatedByString method again. Then you take each
word and put it into an instance of your person object. Create one
instance of person for each line... add those to an NSArray.
NOTE: This is 'edited' in the e-mail message never compiled code that
was copied from a working Foundation tool.
ALSO NOTE: The @"\n" is just a guess as to what the end of line marker
is for your file... but it's probably right.
So, the classes you need to look at to solve your immediate needs are
only:
NSString
NSArray
You could also make life a little harder on your self and use
NSFileHandle and the NSData classes and do the reading from the file
yourself... NSString's initWithContentsOfFile is pretty darn handy
though.
You're probably also soon going to have to learn
retain/release/autorelease.
I haven't solved the whole problem for you... but hopefully this will
nudge you in the right direction. Usually when you read and write to a
file you just use the NSCoding protocol and IB to handle most of the
grungey stuff.
+= Jeff Knee
PS - TOTAL SIDE NOTE: Has anyone else not received a digest copy of
the mailing list today? I found this on mamasam archive and replied to
it that hard (manual) way...
_______________________________________________
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.