Dario
Dario

Reputation: 109

Read from txt list file and set many objects in ios

I, I'm writing an application that has to read the content of txt. this txt is such a property file with a list formatted in this way:

1|Chapter 1|30

2|Chapter AA|7

3|Story of the United States|13

........

keys are separated by "|".

I googled a lot hoping to find any "pragmatically solution" but nothing... how can I read these informations and set many objects like:

for    NSInterger *nChapter = the first element

for    NSString *title = the second element

for    NSInteger *nOfPages = the last element ?

Upvotes: 0

Views: 89

Answers (2)

user529758
user529758

Reputation:

Only if you read NSString's class reference:

NSString *str = [NSString stringWithContentsOfFile:@"file.txt"];
NSArray *rows = [str componentsSeparatedByString:@"\n"];

for (NSString *row in rows)
{
    NSArray *fields = [row componentsSeparatedByString:@"|"];
    NSInteger nChapter = [[fields objectAtIndex:0] intValue];
    NSString *title = [fields objectAtIndex:1];

    // process them in here
}

Upvotes: 0

Related Questions