0

I need help reading in a .txt file into a NSMutableArray in Xcode. I want to read in a large file containing many different strings and integers in a specific order, then I want to create a new person object with the information read in and add each object to an array of person objects.

So if this was my text file:

Richard
Smith
richardsmitha@gmail.com
18
www.richardsmith.com
Steve
Jobs
stevejobs@apple.com
12
www.stevejobs.com

I want a method to assign the following:

firstName = Richard
lastName = Smith
email = richardsmith@gmail.com
age = 18
website = www.richardsmith@gmail.com

Then is should create the object with those values.

PersonObject *person = [[PersonObject alloc] init];
temp = [PersonObject createPerson:firstName:lastName:email:age:website];

NSMutableArray *people = [[NSMutableArray alloc] initWithObjects:PersonObject];
[people addObject:temp];

Then it should repeat for however many people there are in the .txt file.

Anne
  • 26,765
  • 9
  • 65
  • 71
  • i dont see how your going to do that without a delimiter. You could probably parse out the email addresses easy enough but there is no way to know where the end of the first name is and the start of the last name. not to mention that some emails wont be a concatenation of their name – owen gerig Jun 19 '12 at 20:23
  • There is a delimiter; return characters, but the formatting was hosed. Fixed. – bbum Jun 19 '12 at 20:27

1 Answers1

0

here is how you would read the file

NSString* path = [[NSBundle mainBundle] pathForResource:@"filename" 
                                                 ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

parsing stuff out would be like

NSString * emailAddress = [content substringToIndex:[content rangeOfString:@".com"].location]

emailAddress = [content substringFromIndex:[content rangeOfString:@" "].location]

after your last update you might want to look for \n instead of " ". however you can also just nslog out the content to see exactly what your delimiters look like

owen gerig
  • 6,165
  • 6
  • 52
  • 91