views:

116

answers:

2

I'd like to load a file from a specific Group in Xcode/Objective-c

for example:

I have TWO files named "SomeFile.txt" that are in two different folders (folders not groups yet) in the OS:

SomeFolderOne |- SomeFile.txt

SomeFolderTwo |- SomeFile.txt

Inner Xcode I make two folders, and I put a REFERENCE to these two files:

SomeGroupOne |- SomeFile.txt // this file is a reference to the SomeFile.txt from SomeFolderOne

SomeGroupTwo |- SomeFile.txt // this file is a reference to the SomeFile.txt from SomeFolderTwo

Now I want to read the txt content with:

NSString *contents = [NSString stringWithContentsOfFile:@"SomeFile.tx" encoding:NSUTF8StringEncoding error:nil];

Ok it reads the 'SomeFile.txt' but sometimes the file read is from SomeGroupOne and sometimes the file is read from SomeGroupTwo.

How to specify the group I want the file to be read?

A: 

Add both SomeFolderOne and SomeFolderTwo to the Xcode project and ensure you select the Create Folder References for any added folders option.

This will cause the actual folders and their contents to be copied into your app bundle's Resources folder.

You can then access them in your app by doing something like:

NSString* path = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"txt" inDirectory:@"SomeFolderOne"];
NSString* contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
Rob Keniger
A: 

Beware if you're on the iPhone platform that regardless of your directory structure for resources in the project, all those files will get put in the root of the bundle directory anyways. I'm not sure what will happen if you have multiple files of the same name.

quixoto
Good point. As the original poster hasn't specified Mac or iPhone I've answered as a Mac developer. I believe that what happens on the iPhone is that only one of the files with the same name makes it into the bundle, so the answer is to make sure the files have different names.
Rob Keniger