我需要在iPhone应用程序中访问应用程序构建的文件(.plist等)。这是一种硬编码方式:
NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
objectAtIndex:0]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:appFolder];
其中appFolder
是文件夹应用的名称,例如“test.app”。知道appDir之后,访问文件很简单。
是否还有其他非硬编码方式可以从应用程序访问文件?
提前致谢!
答案 0 :(得分:4)
NSString* pathToFile =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"myFile.plist"];
// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
ofType:@"plist"];
答案 1 :(得分:1)
您的app文件夹是“主要包”。所以你可以使用NSBundle方法,如
NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];