-1

我有一个相当大的 plist,我正在尝试从中加载数据。这是 plist 的一个示例,但是有 580 条记录,所以我不能把它们都放在这里:

<plist version="1.0">
    <array>
            <dict>
                    <key>Grave #</key>
                    <string></string>
                    <key>Last Name</key>
                    <string>?</string>
                    <key>First Name</key>
                    <string>Ada Lou daughter of</string>
                    <key>Dates</key>
                    <string>?-? Unable to read stone</string>
                    <key>Notes</key>
                    <string></string>
                    <key></key>
                    <string></string>
            </dict>
            <dict>
                    <key>Grave #</key>
                    <string></string>
                    <key>Last Name</key>
                    <string>?</string>
                    <key>First Name</key>
                    <string>Stone w/ Cherokee syllabry and also in english says: Here we rest</string>
                    <key>Dates</key>
                    <string></string>
                    <key>Notes</key>
                    <string></string>
                    <key></key>
                    <string></string>
            </dict>

我的 .h 文件中有我的财产

@property (nonatomic, strong) NSDictionary *graves;

在我的 .m 文件中合成:

@synthesize graves;

这是我在我的视图中加载文件的代码确实加载功能:

NSString *file = [[NSBundle mainBundle] pathForResource:@"RossCemeteryList" ofType:@"plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:file]) {
    NSLog(@"The file exists");
} else {
    NSLog(@"The file does not exist");
}

graves = [[NSDictionary alloc] initWithContentsOfFile:file];
NSLog(@"%i", [graves count]);

NSLog 消息说它找到了文件,第二个 NSLog 消息中有 0 行。我可以尝试使用 "NSLog (@"%@",graves);" 吐出内容坟墓。它返回NULL。

我有点失落。任何帮助表示赞赏。

4

2 回答 2

2

您正在尝试创建一个 NSDictionary 对象并从该 plist 的内容对其进行初始化,而该 plist 实际上包含一个 NSArray 作为其根对象。您需要将 NSDictionary 更改为 NSArray 或 NSMutableArray。

于 2013-07-29T21:35:41.597 回答
1

我不知道您是否仍在寻找解决方案,但此代码应该允许您加载 plist 文件并确定其内容的类型:

//  load a Property List ("plist") file (fully-qualified path) into a generic object, if
//  it's available...
- ( NSObject * ) testLoadPListData: ( NSString * const ) plistPath

{

    //  load the file data into a raw data object...
    NSData * const data = [ NSData dataWithContentsOfFile: plistPath ];

    //  fields returned from property list creation...
    NSPropertyListFormat fmt = 0;
    NSError * err = nil;

    //  load the file data into a serialized property list object...
    NSPropertyListSerialization * plist = [ NSPropertyListSerialization propertyListWithData: data
                                                                                     options: NSPropertyListImmutable
                                                                                      format: &fmt
                                                                                       error: &err
                                          ];

    //  if there was an error creating the serialized object...
    NSString * const errorText = err ? [ err description ] : nil;
    if ( errorText )

        {

        //  log the error string...
        NSLog( @"error while reading data from file \"%@\": %@"
             , plistPath
             , errorText
             );

        }  // end error creating serialized object

#if defined( DEBUG )

    //////////////////////////////
    //                          //
    //  DEBUG PURPOSES ONLY...  //
    //                          //
    //////////////////////////////

    //  if this file is in a format that can be readily translated into one of our target data types...
    if (  plist )

        {

        //  if this property list file contains a dictionary...
        if ( [ plist isKindOfClass: [ NSDictionary class ] ] )

            {

            //  dump the contents of the dictionary to the debug output window...
            NSDictionary * const dict = ( NSDictionary * )( plist );
            __CCLOG( @"<Dictionary>%@\n</Dictionary>", dict );

            }  // end is dictionary plist file

        //  if this property list file contains an array...
        else if ( [ plist isKindOfClass: [ NSArray class ] ] )

            {

            //  dump the contents of the array to the debug output window...
            NSArray * const arr = ( NSArray * )( plist );
            __CCLOG( @"<Array>%@</Array>", arr );

            }  // end is array plist file

        }  // end valid file format

#endif  // defined( DEBUG )

    return plist;

}  //  end testLoadPListData
于 2014-06-22T04:28:42.380 回答