VansFannel
VansFannel

Reputation: 45921

Can't access resources file

I'm developing an iOS application and I want to load two CSV files into my app.

As you can see in the following image, those files are in Resources group:

enter image description here

I use this to get its file path:

filePath = [[NSBundle mainBundle] pathForResource:@"Familias" ofType:@"txt"];

And this is the code for Util readTitleFromCSV:

+(void)readTitleFromCSV:(NSString*)path Entity:(NSString*)entityName
{
    AppDelegate* appDelegate =
        (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSString *fileDataString =
        [NSString stringWithContentsOfFile:path
                                  encoding:NSUTF8StringEncoding
                                     error:nil];

    NSArray *linesArray = [fileDataString componentsSeparatedByString:@"\n"];

    [ ... ]

}

But fileDataString is nil and I don't get any error.

What am I doing wrong?

Upvotes: 1

Views: 399

Answers (1)

sergio
sergio

Reputation: 69027

Pass an NSError into your file and check why the call is failing. Likely, there is some encoding issue.

Before that, check that the file is included in your app bundle by going to your target build phases pane (see image below for guidance)

enter image description here

Upvotes: 1

Related Questions