开发者

Load a file in a group objective-c Xcode

I'd like to l开发者_开发知识库oad 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?


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜