Plist不能读...我做错了什么? [英] Plist cannot be read ... What am I doing wrong?
问题描述
我有一个plist在我的资源文件夹称为levelconfig.plist,我想读一些东西。
I have got a plist in my Resources folder called "levelconfig.plist" and I want to read something out of it.
我做了以下操作:
-(NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"levelconfig.plist"];
}
-(void) readPlist{
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@",array);
NSLog(@"%@", filePath);
}
}
在ccTouchesBegan方法中,我调用:
And in the ccTouchesBegan method I call :
[self readPlist];
我的plist包含一个数组,应该显示正确吗?将级别数据存储在.plist文件中是个好主意?
My plist contains an array, that should be displayed right ? Is it a good idea to store level data in a .plist file ?
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">
<dict>
<key>Root</key>
<array>
<string>sunday</string>
<string>monday</string>
<integer>44</integer>
</array>
</dict>
</plist>
推荐答案
您必须先阅读plist中指定的字典。例如:
You must first read the dictionary as specified in your plist. For example:
NSString *mainPath = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [mainPath stringByAppendingPathComponent:@"test.plist"];
NSDictionary * itemPositions = [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [itemPositions objectForKey:@"Root"];
NSLog(@"%@", items);
我不知道你在使用其他代码做什么,但两行足以加载文件。请原谅变量名;我很快适应这从我目前的项目。另外,请确保在dealloc或适当的地方释放数组和字典。
I'm not sure what you are doing with your other code but two lines suffice to load the file. Please excuse the variable names; I quickly adapted this from my current project. Also, make sure you release the Array and Dictionary in dealloc or wherever appropriate.
这篇关于Plist不能读...我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!