0

In my iOS app, I have it set to read a .txt file from the resources folder, and create an NSArray from each line of text. However, when I try this, the NSArray returns empty.

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"LUKEQUESTIONSPLAINTEXTFILE" ofType:@"txt"];
    NSData* data = [NSData dataWithContentsOfFile:filepath];
    NSString* string = [[NSString alloc] initWithBytes:[data bytes]
                                                length:[data length]
                                              encoding:NSUTF8StringEncoding];

    NSArray *listArray = [string componentsSeparatedByString:@"\n"];
    NSLog(@"HERE IT IS: %@", listArray);

Moreover, if I do a lot for the entire string, it stops after the very first line break. What am I doing wrong? The file is only 214 bytes.

user717452
  • 33
  • 14
  • 73
  • 149
  • It should be `\n` not `/n`... – l'L'l Sep 04 '19 at 17:34
  • @l'L'l same result, updated code to reflect the change. Plus, even when I log the value 'string' I only get until the first line break, as though it can't see anything after a line break. – user717452 Sep 04 '19 at 17:36
  • Maybe try instead using `NSCharacterSet *separator = [NSCharacterSet newlineCharacterSet];` then `NSArray *listArray = [string componentsSeparatedByCharactersInSet:separator];` – l'L'l Sep 04 '19 at 17:40
  • This may be a better approach https://stackoverflow.com/a/16223139/6257435 – DonMag Sep 04 '19 at 17:42
  • @DonMag I did that, and contents came back blank, but the count for the array did show 3072, it's just not showing the array in NSLog is maybe the issue? It's been a pain, I'm just trying to get this text file converted into a PLIST file, where each question and answer choice in the file is a entry in a dict file of the PLIST – user717452 Sep 04 '19 at 17:45
  • @user717452 - I just did a quick test using the code in the answer I linked to, and it worked fine. Is it possible your `.txt` file is malformed? – DonMag Sep 04 '19 at 17:51
  • It seems to be the issue that someone marked with it truncating text, as I can manually pull an objectAtIndex of the array up.....just wish I had been aware of this before debugging for over an hour lol. SMH oh well – user717452 Sep 04 '19 at 18:32

0 Answers0