Bushes
Bushes

Reputation: 1010

Load text file from specific location objective c

I was wondering how to read a file from a specific location on ios.

For example I'd quite like to have files of the same name stored in different locations for example.

Project/Files/Text/1.txt

Project/Files/MoreText/1.txt

At the moment I can load files in but I can't see how to specify a specific directory. My program seems to load a file in regardless of where it is meaning I can only have one text file or the other working.

I have tried using NSFileManager:

NSFileManager *filemanager = [NSFileManager defaultManager];

NSArray *filelist = [filemanager directoryContentsAtPath:@"Text"];

but I later realised that this actually isn't doing anything. It just returns no objects. I've also seen NSBundle but haven't seen an example where a specific file is returned.

I need the filenames to be the same for my algorithm which loads the files.

Any help would be appreciated.

Upvotes: 8

Views: 13234

Answers (4)

syclonefx
syclonefx

Reputation: 2990

If you want to read files from the app bundle and you want a certain directory structure, then you need to create a directory in Finder. Then create the directory structure you want in the main directory you just made. Now add the files to the appropriate folders and then add the main directory to your project by dragging it in to Xcode.

Here is the code to read from the main bundle:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Text/1" ofType:@"txt"];
    NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",testString); 

This will read the file "1.txt" from the directory "Text".

Upvotes: 16

virushuo
virushuo

Reputation: 660

iOS application will save all file to root directory, a Group(yellow icon) not a real directory. If you want put your resources into a sub directory, can using this following steps:

  1. Create a directory in the Finder, put your files into this directory.
  2. In Xcode, select "Add Files to..." from File menu.
  3. Select "Copy items into destination group's folder" and "Create Folder References for any added folders" in the popup.

Then, you will found a blue icon directory in your project, It is a real directory.

Now, you can read the files use NSBundle Class method:

+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath

Upvotes: 2

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Relative paths don't work on Mac OS X--this is because the current directory is ignored by Cocoa, and changes pretty nearly at random. You need to always work with an absolute path. To get the absolute path to your app's Resources directory, use:

NSString *resDir = [[NSBundle mainBundle] resourcePath];

Then to enumerate the contents of the Text directory in that directory:

NSString *textDir = [resDir stringByAppendingPathComponent: @"Text"];
NSArray *textFiles = [[NSFileManager defaultManager] directoryContentsAtPath: textDir];

However, Xcode may or may not honour the file hierarchy in your app's Resources directory. To determine if files are where you expect them to be, you should build your app, right-click the .app bundle, and select Show Package Contents.

Upvotes: 0

RolandasR
RolandasR

Reputation: 3047

first of all, keep in mind that by default all files goes to 1 directory after compiling. To put them to directory you have to specify that in xCode. As for loading goes you can just enter relative path "MyDir/myFile.txt"

Upvotes: 0

Related Questions