我有一个相当大的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。
我有点失落。任何帮助表示赞赏。
答案 0 :(得分:2)
您正在尝试创建一个NSDictionary对象并从该plist的内容初始化它,而该plist实际上包含一个NSArray作为其根对象。您需要将NSDictionary更改为NSArray或NSMutableArray。
答案 1 :(得分: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