I tried to run this code for reading a text file but it doesn't work.

Hi,

I tried to run this code for reading a text file but it doesn't work. Do you see anything wrong with the code?



#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bob" ofType:@"sm"];
        NSError *error;
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
       
        if (error)
            NSLog(@"Error reading file: %@", error.localizedDescription);
       
       
        NSString *delimiter = @"\n";
       
        NSArray *listArray = [string componentsSeparatedByString:delimiter];
       
        NSLog(@"items = %lu", (unsigned long)[listArray count]);
       
        for (int i = 0; i < listArray.count; i++) {
            NSLog(@"%@", listArray[i]);
        }
       
    }
    return 0;
}

Replies

Can you be more explicit what "doesn't work" means? Crashes? Logs an exception message and crashes? Produces the wrong string from the file? Fails to decompose into lines?