Iphone 阅读怪异行为
我在应用程序中使用一系列代码来读取Plist文件,但是出现了一种奇怪的行为。下面是一个代码示例Iphone 阅读怪异行为,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我在应用程序中使用一系列代码来读取Plist文件,但是出现了一种奇怪的行为。下面是一个代码示例 //init loading from PLIST here, and then associate value. NSString *pathString=[NSString stringWithFormat:@"%@Levels",targetWorld]; NSString *path = [[NSBundle mainBundle] pathForResource:pathString ofT
//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>
一级
世界尺寸
宽度
123
高度
344
形成
playerPosX
0
戏班
0
您将快速枚举与
n分子混为一谈
使用NSEnumerator并使用while((object=[enumerator nextObject]){
,或者在级别
对象上使用快速枚举(无需创建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);
}
与:
这样行吗
另请参见您已经描述了plist的数据结构,也没有显示它实际包含的内容。如何记录level
和worldSize
。第二个dictionary-in-a-dictionary的“宽度”和“高度”键为0,或者根本没有这些键。在这种情况下,-objectForKey:
将返回nil
,-intValue
(或任何发送到nil
的消息)将给出0。您应该显示您的输入plist文件。问题已更新,以我的plist的示例形式出现。我记录了级别和世界大小,所有结果都正确无误。我确信宽度和高度不是0,因为它确实在第一个日志中打印出来了。日志记录程序显示,它们都在第一对日志之后立即返回到0。无法。。立即抛出错误。'NSInvalidArgumentException',原因:'-[\uu NSCFString objectForKey:]:未识别的选择器发送到实例0x10738ba0'好的,您应该注意为什么您要对NSEnumeration执行快速枚举,但正如我之前指出的,这可能与您的问题有关。是的,看起来是这样的。。但我不知道为什么没有NSEmurator它就不能让我通过。我做了很多研究,但没有发现任何与我类似的问题。我想我现在应该试着弄清楚为什么for循环循环两次,然后在第二次循环时将值返回到0
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);
}