我正在使用一系列的代码来读取我的应用程序中的Plist文件,但是一个奇怪的行为开始了。这里是一个代码示例。Plist阅读怪异行为
//init loading from PLIST here, and then associate value.
NSString *pathString=[NSString stringWithFormat:@"%@Levels",targetWorld];
NSString *path = [[NSBundle mainBundle] pathForResource:pathString ofType:@"plist"];
NSString *levelNameNumber = [[NSString alloc] initWithFormat:targetLevel];
NSDictionary *levelsList = [[NSDictionary alloc] initWithContentsOfFile:path];
NSDictionary *level = [levelsList objectForKey:levelNameNumber];
NSEnumerator *levelFormations = [level objectEnumerator];
for(NSDictionary *worldSize in levelFormations)
{
worldWidth = [[worldSize objectForKey:@"width"] intValue];
worldHeight = [[worldSize objectForKey:@"height"] intValue];
NSLog(@"height is %d",worldHeight);
NSLog(@"width is %d",worldWidth);
}
[levelNameNumber release];
[levelsList release];
正如所示,我已经设置了NSLog以保持值的观察。这里是日志
2012-05-09 21:14:38.313 CapNTail[361:10a03] height is 344
2012-05-09 21:14:38.315 CapNTail[361:10a03] width is 123
2012-05-09 21:14:38.324 CapNTail[361:10a03] height is 0
2012-05-09 21:14:38.326 CapNTail[361:10a03] width is 0
他们似乎将自己重置为零。我能得到的最好的结果是它可能是for循环引起的,因为两个日志都被打印了两次(看起来像是运行第二个循环并重置它)。任何想法有什么不对?我的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>level1</key>
<dict>
<key>worldSize</key>
<dict>
<key>width</key>
<integer>123</integer>
<key>height</key>
<integer>344</integer>
</dict>
<key>formation</key>
<dict>
<key>playerPosX</key>
<integer>0</integer>
<key>playerPosY</key>
<integer>0</integer>
</dict>
</dict>
你描述你的plist的数据结构,也不表示它实际包含的内容。如何记录'level'和'worldSize'。字典中的第二个字典对于“宽度”和“高度”键都有0,或者根本没有这些键。在这种情况下,'-objectForKey:'将返回'nil'和'-intValue'(或任何消息)发送给'nil'将给出0. –
您应该显示您的输入plist文件。 – occulus
问题更新,例如形成我的plist。我记录了level和worldSize,一切都正确。我相信宽度和高度不是0,因为它确实在第一个日志中打印出来。记录器显示它们在第一对记录之后立即返回到0。 – Bek