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"];
}
NSMutableArray * lines = [[NSMutableArray alloc] initWithArray:[filePath componentsSeparatedByString:@"\n"] copyItems: YES];
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.