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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.