我想在不同的文件路径中访问数据(xml,具体而言),具体取决于某些参数。简而言之,还有一个/ Saves /文件夹,其中包含其他几个文件夹(/ Save1 /,/ Save2 /,/ Save3 /等)。我希望能够解析。
让我们假设我要访问的文件位于以下路径中:
/../projectname/Saves/Save1/dialogue.xml
在 finder 中,我创建了文件夹子结构,然后将其复制到我的XCode项目中。这意味着,这些文件夹存在于文件结构中,而不仅仅存在于XCode中。 之后我试着做以下事情:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:"@/Saves/Save1/dialogue.xml"];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
但是,这似乎不能正常工作。记录路径变量我得到以下内容:
/Users/<username>/Library/Application Support/iPhone Simulator/5.1/Application/<appcode>/<appname>.app/Saves/Save1/dialogue.xml
我的错误在哪里?提前感谢您的帮助。
答案 0 :(得分:1)
请确保您在项目目标中也添加了相同的文件夹。您可以在项目目标下添加相同的结构并编译,然后在.app文件中创建相同的文件夹。然后你可以按照你提到的方式访问它。
答案 1 :(得分:0)
试试这个:
//NSString *path = [[NSBundle mainBundle] pathForResource:@"dialogue" ofType:@"xml" inDirectory:@"Saves/Save1"];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Saves/Save1/dialogue.xml"];
if([[NSFileManager defaultManager] fileExitsAtPath:path])
{
NSMutableData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
}
else
{
NSLog(@"File doesnot exits");
}