Re: Files and arrays
Re: Files and arrays
- Subject: Re: Files and arrays
- From: Jim Balhoff <email@hidden>
- Date: Fri, 15 Nov 2002 20:25:29 -0500
I'm still a newbie for sure, but I'll give it a shot:
// caution - typed into Mail by a beginning programmer!
NSString *fileContents = [[NSString alloc]
initWithContentsOfFile:aFile];
NSArray *peopleArray = [fileContents componentsSeparatedByString:@"\n"];
NSEnumerator *enumerator = [peopleArray objectEnumerator];
NSString *nextPersonString;
NSMutableArray *personArray = [[NSMutableArray alloc] init];
while (nextPersonString = [enumerator nextObject]) {
Person *aPerson = [[Person alloc] initWithString:nextPersonString];
[personArray addObject:aPerson];
}
You could either have an initializer for Person that took an NSString
argument as above and then used a componentsSeparatedByString:@"," to
get an array with the values, or use componentsSeparatedByString: @","
ahead of time and pass the initializer an NSArray instead.
Hope this helps,
Jim Balhoff
On Thursday, November 14, 2002, at 04:27 PM, Mike Brinkman 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.
I want to create an array and initialize it with the contents of a
comma separated text file. There are 10 records in the text file, and
at the end of each record there is a return character (this file was
created in TextEdit in OS X, so I'm not sure what the return character
is).
I have a class Person and it's defined as:
@interface Person : NSObject {
NSString *personName;
NSString *personPic;
NSString *sex;
int age, height, weight;
}
// accessors
- (void)setPersonName:(NSString *)pName;
- (void)setPersonPic:(NSString *)pPic;
- (void)setPersonSex:(NSString *)pSex;
- (void)setAge:(int)pAge;
- (void)setHeight:(int)pHeight;
- (void)setWeight:(int)pWeight;
How would I create an array of Person and make each value go into the
correct value?
Thanks for any help. Sorry once again for asking, but I haven't been
able to figure it out on my own.
_______________________________________________
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.