简体   繁体   中英

Reading from text file in objective-c

I have a textfile which consists of multiple words. They are all separated by new lines.

What Ive tried:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];

if (filePath) { // This does trigger, so the file is actually found.

    NSArray *arr = [filePath componentsSeparatedByString:@"\n"]; 

}

What Ive also tried:

NSMutableArray * lines = [[NSMutableArray alloc] initWithArray:[filePath componentsSeparatedByString:@"\n"] copyItems: YES];

Neither of these seems to work as I only seem to get the filepath when I read out lines using NSLog. Can any of you help?

The lines array only seem to consist of one object, the string of the location of the textfile.

I want an array where each string is a string separated by a line in the textile.

First of all you need to get the file within your application bundle. That means, you must put it inside your application's 'Resources' folder.

Then you can read it like this:

NSString* path = [[NSBundle mainBundle] pathForResource:@"filename" 
                                                 ofType:@"txt"];

Then loading the content into a NSString is even easier:

NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

Then create the array:

NSArray *arr = [content componentsSeparatedByString:@"\n"]; //might also be @"\r" check both.

All your doing there is hacking u the filePath - you havent loaded any data from the file.

Lots of ways to do it. [NSData initWithContentsOfURL... might be a start

The method [NSString componentsSeparatedByString:] is used on the string itself, that is in your case the file path, and not the content of the file at this path.

There is a lot of ways to read from a file. Dave DeLong has posted a clever approach for this problem. Check it here .

NSString * stringFileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@“yourfilename”ofType:@“txt”] encoding:NSUTF8StringEncoding error:&error];

Your error is that you confuse the path - a string that merely tells you WHERE is your file located in the file-system (somewhere within your application's bundle in this case) and the actual file - and its content, which are different thing.

Instead of breaking the CONTENTS of your file into lines, you broke the PATH leading to the file.

So, a better implementation would start like yours:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];

But continue by reading the file contents into a single string:

NSString *fileContents = [NSString stringWithContentsOfFile:filePath];
    

And only then attempt to break the file contents into separate strings, each holding one line of text, using all official line-breakers.

NSArray *textLines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Of course its better to check at each stage that previous stages succeeded.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM