【问题标题】:How to read data structure from .plist file into NSArray如何将 .plist 文件中的数据结构读入 NSArray
【发布时间】:2010-10-19 11:14:38
【问题描述】:

我正在使用以下内容手动创建数据结构:

NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Barclays Premier League", @"name",
                 @"Premier League", @"shortname",
                 @"101", @"id", nil];
NSDictionary* league2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Coca-Cola Championship", @"name",
                 @"Championship", @"shortname",
                 @"102", @"id", nil];
NSDictionary* league3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish Premier League", @"name",
                 @"SPL", @"shortname",
                 @"201", @"id", nil];
NSDictionary* league4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Champions League", @"name",
                 @"UCL", @"shortname",
                 @"501", @"id", nil];

contentArray = [[NSArray alloc] initWithObjects:

                [[NSDictionary alloc] initWithObjectsAndKeys: @"English", @"category", [[NSArray alloc] initWithObjects: league1, league2, nil], @"leagues", nil],
                [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish", @"category", [[NSArray alloc] initWithObjects: league3, nil], @"leagues", nil],
                [[NSDictionary alloc] initWithObjectsAndKeys: @"Tournaments", @"category", [[NSArray alloc] initWithObjects: league4, nil], @"leagues", nil],
                nil];

[league1 release];
[league2 release];
[league3 release];
[league4 release];

但是,我认为如果从文件中读取它会更好。所以我创建了具有以下结构的文件leagues.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <dict>
            <key>category</key>
            <string>English</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Barclays Premier League</string>
                    <key>shortname</key>
                    <string>Premier League</string>
                    <key>id</key>
                    <string>101</string>
                </dict>
                <dict>
                    <key>name</key>
                    <string>Coca-Cola Championship</string>
                    <key>shortname</key>
                    <string>Championship</string>
                    <key>id</key>
                    <string>102</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>category</key>
            <string>Scottish</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Scottish Premier League</string>
                    <key>shortname</key>
                    <string>SPL</string>
                    <key>id</key>
                    <string>201</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>category</key>
            <string>Tournaments</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Champions League</string>
                    <key>shortname</key>
                    <string>UCL</string>
                    <key>id</key>
                    <string>501</string>
                </dict>
            </array>
        </dict>
    </array>
</plist>

我如何读入这个文件。我尝试了各种方法,但没有任何效果。我什至不知道我是否在寻找文件的正确位置。作为参考,我正在尝试以下方法:

NSString* errorDesc = nil;
NSPropertyListFormat format;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
NSData* plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
contentArray = (NSArray*)[NSPropertyListSerialization
                                     propertyListFromData:plistXML
                                     mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                     format:&format
                                     errorDescription:&errorDesc];

if (!contentArray) {
    NSLog(errorDesc);
    [errorDesc release];
}

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"leagues.plist"];
NSLog(fooPath);
contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(@"%@",contentArray);

这终于把我逼疯了。请帮忙!

谢谢你

【问题讨论】:

    标签: ios objective-c iphone cocoa-touch plist


    【解决方案1】:
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
    contentDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    

    该答案是正确的 - 您确定您的文件在应用程序中吗?您是否将它添加到您的项目中,并检查它是否被复制到您的应用程序包中?如果不是,则可能是文件未添加到您正在构建的目标中,这是一个容易犯的错误,尤其是在您有多个目标的情况下。

    【讨论】:

    • 只是在这里学习:)。我不知道它是否在主包中。谢谢,我会检查下。
    • 它是:[NSDictionary dictionaryWithContentsOfFile:plistPath];
    • contentArray =>> 应该是 contentDict
    • 感谢@TàTruhoada,已修复。
    【解决方案2】:

    如果plist 位于项目的资源文件夹中,请使用此代码。

      NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"league"    ofType:@"plist"];
     contentArray = [NSArray arrayWithContentsOfFile:sourcePath];
    

    如果plist 在应用程序的文档目录中,请使用:

        NSArray *paths = 
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    
    NSString *plistName = @"league";
    NSString *finalPath = [basePath stringByAppendingPathComponent: 
                           [NSString stringWithFormat: @"%@.plist", plistName]];
    
    
    
    contentArray = [NSArray arrayWithContentsOfFile:finalPath];
    

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,但它不起作用,因为我将 pList 中的结果放入应该是字典的数组中,即

      NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"VehicleDetailItems" ofType:@"plist"];
      NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
      

      【讨论】:

        【解决方案4】:

        肯德尔是正确的。

        根据我的经验,您需要将文件添加到 xcode 中的“资源”文件夹中。

        【讨论】:

        • Resources 文件夹中的文件夹呢?那里也可以加吗?
        • @Patricia 没关系,只要确保它与组相关。
        【解决方案5】:

        为了完整起见,肯德尔和本特福德是完全正确的。但是,在我的示例中,contentArray 是一个属性,并且在方法结束时它超出了范围,因为 arrayWithContentsOfFile 创建了一个自动释放的对象。

        为了使这项工作正常进行,我需要做 3 件事:

        1. 将文件放到资源文件夹中

        2. 正确命名文件(是leagues.plist 而不是league.plist)

        3. 使用 [[NSArray alloc] initWithContentsOfFile:plistPath)] 读取文件;

        第三部分创建了一个分配的 NSArray,当你退出这个函数的作用域时,它不会释放……当然,这需要在 dealloc 函数中释放。

        【讨论】:

        • 对于自动释放的东西..你可以在适当的地方“保留”。
        【解决方案6】:

        只是添加。我遇到了同样的问题,建议的解决方案帮助我解决了问题,但是我不确定我是否真的使用了确切的解决方案。在我的情况下,问题是 .plist 文件被添加到不同的目标(之前添加了一个新目标)。因此解决方案是 .plist > Get Info > Targets,并确保将其添加到正确的目标,以便在安装时将其复制到设备。该死的,如果我尽快弄清楚这一点,我会节省很多时间。希望这也有帮助。问候!

        【讨论】:

          【解决方案7】:

          如果文件没有添加到资源文件夹中,而只放在您的文档目录中。 你可以这样做

          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.plist"];
          NSMutableArray *arrContentsplist = [[NSMutableArray alloc] initWithContentsOfFile:path];
          

          【讨论】:

            【解决方案8】:

            此代码有效

            NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plist-name" ofType:@"plist"];
            NSDictionary *Dictionary= [[NSDictionary alloc]initWithContentsOfFile:plistPath];
            NSLog(@" current version CFBundleVersion = %@",[Dictionary valueForKey:@"CFBundleVersion"]);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-04-03
              • 1970-01-01
              • 1970-01-01
              • 2023-03-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多