0

I am using a series of code to read Plist file in my app, However a weird behavior kicks in. Here is an example of the code.

//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];

As it've shown, i've set up NSLog to keep watch of the value. Here is the log

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

They seems to reset themselves back to zero. The best i could get so far is it might be the for loop is causing this up since both logs have gotten printed twice( Seems like it runs a second loop and reset it.) Any idea what's wrong?

Example code of my 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>

Bek
  • 31
  • 1
  • 6
  • 2
    You have described the data structure of your plist nor shown what it actually contains. How about logging `level` and `worldSize`. Either the second dictionary-within-a-dictionary has 0 for the "width" and "height" keys, or it doesn't have those keys at all. In that case `-objectForKey:` would return `nil` and `-intValue` (or any message) sent to `nil` would give 0. – Ken Thomases May 09 '12 at 13:40
  • You should show your input plist file. – occulus May 09 '12 at 13:46
  • Question updated, with example formation of my plist. I logged both the level and worldSize, everything came out correctly. And i am sure the width and height are not 0 since it did get printed out in the first log. the logger shows both of them were returned to 0 right after the first pair of log. – Bek May 09 '12 at 14:55

1 Answers1

1

You're confusing fast enumeration with an NSEnumerator.

Either use NSEnumerator and loop over it using while ((object = [enumerator nextObject])) {, or use fast enumeration on the level object (no need to make an NSEnumerator).

To try the latter strategy (recommended), replace this:

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

}

with:

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

Does that work?

See also this question.

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • Can't.. got error throw back right away.'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x10738ba0' – Bek May 09 '12 at 15:03
  • Ok, you should pay attention to why it is you're doing a fast enumeration over an NSEnumeration though, as I pointed out before, it may be related to your problem. – occulus May 09 '12 at 16:31
  • Yea, it seems like it.. But i have no idea why it wouldn't let me get through without NSEmurator. I've done quite some research but i can't find any problem that is similar to mine. I think what i should be trying to figure out now is Why did the for loop looped twice and return the value back to 0 at the second loop – Bek May 10 '12 at 06:29