2012-05-09 199 views
0

我正在使用一系列的代码来读取我的应用程序中的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> 

+2

你描述你的plist的数据结构,也不表示它实际包含的内容。如何记录'level'和'worldSize'。字典中的第二个字典对于“宽度”和“高度”键都有0,或者根本没有这些键。在这种情况下,'-objectForKey:'将返回'nil'和'-intValue'(或任何消息)发送给'nil'将给出0. –

+0

您应该显示您的输入plist文件。 – occulus

+0

问题更新,例如形成我的plist。我记录了level和worldSize,一切都正确。我相信宽度和高度不是0,因为它确实在第一个日志中打印出来。记录器显示它们在第一对记录之后立即返回到0。 – Bek

回答

1

你混淆快速列举与NSEnumerator

使用NSEnumerator并使用while ((object = [enumerator nextObject])) {对其进行循环,或在level对象上使用快速枚举(无需制作NSEnumerator)。

要尝试一种策略(推荐),替换此:

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); 

}

有:

for(NSDictionary *worldSize in level) 
{ 
    worldWidth = [[worldSize objectForKey:@"width"] intValue]; 
    worldHeight = [[worldSize objectForKey:@"height"] intValue]; 

    NSLog(@"height is %d",worldHeight); 
    NSLog(@"width is %d",worldWidth); 
} 

运作的?

参见this question.

+0

不能..得到错误马上回来。'NSInvalidArgumentException',原因:' - [__ NSCFString objectForKey:]:无法识别的选择器发送到实例0x10738ba0' – Bek

+0

好的,你应该注意为什么它是你在做一个尽管如此,正如我之前指出的,它可能与您的问题有关。 – occulus

+0

是的,它似乎是..但我不知道为什么它不会让我通过没有NSEmurator通过。我已经做了相当多的研究,但是我找不到任何类似于我的问题。我想我应该试图找出现在是为什么for循环循环两次,并在第二个循环返回值为0 – Bek