在组objective-c Xcode中加载文件

问题描述 投票:0回答:2

我想在Xcode / Objective-c中加载来自特定组的文件

例如:

我在操作系统中有两个名为“SomeFile.txt”的文件,它们位于两个不同的文件夹中(文件夹不是组):

SomeFolderOne | - SomeFile.txt

SomeFolderTwo | - SomeFile.txt

内部Xcode我制作了两个文件夹,并对这两个文件进行了参考:

SomeGroupOne | - SomeFile.txt //这个文件是对SomeFolderOne的SomeFile.txt的引用

SomeGroupTwo | - SomeFile.txt //这个文件是对SomeFolder的SomeFile.txt的引用2

现在我想阅读文本内容:

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

好的,它会读取'SomeFile.txt',但有时读取的文件来自SomeGroupOne,有时文件是从SomeGroupTwo读取的。

如何指定我想要读取文件的组?

objective-c xcode file nsstring
2个回答
3
投票

SomeFolderOneSomeFolderTwo添加到Xcode项目中,并确保为任何添加的文件夹选项创建“创建文件夹引用”。

这将导致实际文件夹及其内容被复制到应用程序包的Res​​ources文件夹中。

然后,您可以通过以下操作在您的应用中访问它们:

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

1
投票

请注意,如果您在iPhone平台上,无论您的项目资源的目录结构如何,所有这些文件都会被放在bundle目录的根目录中。我不确定如果您有多个同名文件会发生什么。

© www.soinside.com 2019 - 2024. All rights reserved.