为什么我无法获取项目中 plist 文件的路径?

发布于 2024-12-05 20:32:25 字数 540 浏览 0 评论 0原文

我正在使用此函数来获取项目中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一绘本一梦想 2024-12-12 20:32:25

检查案件。 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.

权谋诡计 2024-12-12 20:32:25

如果您的 plist 位于子文件夹中,那么您也应该考虑

[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]

通过指定目录来使用。

if by any chance you are having your plist in subfolder, then you should consider using

[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]

by specifying the directory as well.

末骤雨初歇 2024-12-12 20:32:25

试试这个

+ (NSString *) appDataPath{

     NSString *plistPath;
     NSString *rootPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
                                                                   NSUserDomainMask,
                                                                   YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"MyAppSettings.plist"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
          plistPath = [[NSBundle mainBundle] pathForResource:@"MyAppSettings" 
                                                      ofType:@"plist"];
         return plistPath;
    }
    else 
    // No plist found       
}

Try this

+ (NSString *) appDataPath{

     NSString *plistPath;
     NSString *rootPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
                                                                   NSUserDomainMask,
                                                                   YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"MyAppSettings.plist"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
          plistPath = [[NSBundle mainBundle] pathForResource:@"MyAppSettings" 
                                                      ofType:@"plist"];
         return plistPath;
    }
    else 
    // No plist found       
}
野心澎湃 2024-12-12 20:32:25

我正在使用此代码在我的项目中查找 plist 路径。

-(void)CreatePlistPath
{
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    path = [[documentsDirectory stringByAppendingPathComponent:@"data.plist"] retain];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

        [fileManager copyItemAtPath:bundle toPath: path error:&error];
    }
}

I am using this code to find plist path in my project.

-(void)CreatePlistPath
{
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    path = [[documentsDirectory stringByAppendingPathComponent:@"data.plist"] retain];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

        [fileManager copyItemAtPath:bundle toPath: path error:&error];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文