applelover
applelover

Reputation: 136

how to get data from file.txt into an NSMutableArray

I have file.txt data file in my project which is in resource folder.

Now i need to fetch the data from the file and load it into NSMutableArray.

how to approach to it.

Upvotes: 0

Views: 782

Answers (1)

Alex Terente
Alex Terente

Reputation: 12036

First of all you need to read the file. And the you need to separate the string in components.

    NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"txt"] 
                                                     encoding:NSUTF8StringEncoding 
                                                        error:nil];
    NSMutableArray *stringsArray = [NSMutableArray arrayWithArray:[fileString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

    NSLog(@"Array:%@",[stringsArray description]);

Good luck.

Upvotes: 3

Related Questions