【问题标题】:Not able to retrieve info.plist data by -pathForResource method?无法通过 -pathForResource 方法检索 info.plist 数据?
【发布时间】:2013-05-23 13:05:17
【问题描述】:

在我的应用程序中,我无法使用以下代码获取 plist。我正在使用 Xcode 4.5 这是我检索 plist 的代码,

    NSBundle *thisBundle = [NSBundle mainBundle];
    NSDictionary *theDictionary;
    NSString *commonDictionaryPath;
    if (commonDictionaryPath = [thisBundle pathForResource:@"newfonts-Info" ofType:@"plist"])
    {
        theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
    }

注意:-如果我尝试检索文本或 xml 文件,那么上面的代码可以正常工作。

【问题讨论】:

    标签: ios plist nsbundle


    【解决方案1】:

    要将 Info plist 作为字典,您可以这样做:

    NSDictionary *theDictionary = [[NSBundle mainBundle] infoDictionary];
    

    【讨论】:

    • 你的方法不一定是错的,只是比要求的复杂。
    【解决方案2】:

    使用

    [[NSBundle mainBundle] infoDictionary];
    

    在任何捆绑实例上调用 infoDictionary 都会返回一些东西(认为它可能只包含私钥)。

    【讨论】:

      【解决方案3】:

      试试这个代码

       SArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       NSString *documentsPath = [paths objectAtIndex:0];
      
      
       NSString* fooPath = [[NSBundle mainBundle] pathForResource:@"newfonts-Info" ofType:@"plist"];
       NSLog(fooPath);
       contentArray = [NSArray arrayWithContentsOfFile:fooPath];
       NSLog(@"%@",contentArray);
      

      或者这也可以与 NSDictionary 一起使用

       NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"newfonts-Info" ofType:@"plist"];
      
       NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
      
       NSLog(@"%@",dict);
      

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        终于自己找到答案了

        我只是打开文档目录并打开 application.app 文件,然后看到包中显示为 application-Info.plist 的 plist 文件与 Info.plist 一样可见

        所以我更改了上面的代码并检索字典

        NSString *commonDictionaryPath;
        commonDictionaryPath=[[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];
        NSDictionary *info=[NSDictionary dictionaryWithContentsOfFile:commonDictionaryPath];
        NSLog(@"path %@",commonDictionaryPath);
        

        【讨论】:

          猜你喜欢
          • 2019-09-25
          • 2019-04-04
          • 1970-01-01
          • 2019-09-02
          • 1970-01-01
          • 2021-12-06
          • 2019-12-05
          • 2017-02-07
          • 1970-01-01
          相关资源
          最近更新 更多