Top of the page
Close

This is a red error

This is a red error

This is a red error

How to read a file, line by line into the NSArray in Objective-C

Ondrej Rafaj on 2009.09.17 18:18:01

This piece of the code will first, load your file into the NSString and that by using componentsSeparatedByString will parse all loaded lines into the NSArray.
NSString *entireFileInString = [NSString stringWithContentsOfFile:filePath]; // reads file into memory as an NSString
NSArray *lines = [entireFileInString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
for (NSString *line in lines) NSLog([NSString stringWithFormat:@"line: %@", line]);
Be careful while using large files, this can take a lot of memory.

Ondrej Rafaj

Ondrej Rafaj

Independent iPhone developer @ ondrej-rafaj.co.uk

I am available to give you a free quote or start working on your project ... just give me a call or drop a line. Please find all my details on my portfolio site ondrej-rafaj.co.uk

Back to top Comment

Comments ... Why not to get involved!

We had to disable comments till the problem with spam will be solved, sorry for any inconvenience caused.

Back to top

IE SUCKS, DON'T USE IT :) Digg this page