为什么我无法获取项目中 plist 文件的路径?
我正在使用此函数来获取项目中 plist 文件的路径:
+ (NSString *) appDataPath{
NSLog(@"in appDataPath");
NSString *appDPath = nil;
// Get the main bundle for the app.
NSBundle* mainBundle = [NSBundle mainBundle];
if (mainBundle != nil) {
NSLog(@"in appDataPath mainbundle");
appDPath = [mainBundle pathForResource:@"MyAppSettings" ofType:@"plist"];
NSLog(@"appDataPath: %@", appDPath);
}
return appDPath;
}
它进入 if(mainBundle != nil) 但 appDPath 为空。同样的功能正在另一个项目中运行。为什么它在这里不起作用?有没有其他方法可以获取项目中 plist 文件的路径。提前致谢。
I am using this function to get path of a plist file in my project:
+ (NSString *) appDataPath{
NSLog(@"in appDataPath");
NSString *appDPath = nil;
// Get the main bundle for the app.
NSBundle* mainBundle = [NSBundle mainBundle];
if (mainBundle != nil) {
NSLog(@"in appDataPath mainbundle");
appDPath = [mainBundle pathForResource:@"MyAppSettings" ofType:@"plist"];
NSLog(@"appDataPath: %@", appDPath);
}
return appDPath;
}
It goes in if(mainBundle != nil) but appDPath is null. This same function is working in one other project. Why is it not working here? Is there any other way to get path of a plist file in project. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查案件。 iOS 的文件系统区分大小写。
检查该文件不在目录中。在这种情况下,您需要调用
pathForResource:ofType:inDirectory
。检查该文件是否已为您正在构建的目标启用。如果不是,则不会将其复制到捆绑包中。
Check the case. iOS' filesystem is case sensitive.
Check the file isn't in a directory. You need to call
pathForResource:ofType:inDirectory
in that case.Check the file is enabled for the target you are building. If it isn't, it won't get copied into the bundle.
如果您的 plist 位于子文件夹中,那么您也应该考虑
通过指定目录来使用。
if by any chance you are having your plist in subfolder, then you should consider using
by specifying the directory as well.
试试这个
Try this
我正在使用此代码在我的项目中查找 plist 路径。
I am using this code to find plist path in my project.