开发者

My Dictionary returns memory address instead of actual value, intValue cast not working - objective-C

I have seen some similar problems, but the fix suggested in most does not work for me.

When my game launches, it calls a method, that reads my Plist to find the key "gameProgress" and if it is the first time the game is run, it creates the Plist and add the key "gameProgress" with the value of 0. It looks like this:

else {
  // create it
  NSLog(@"dictionary didn't exist, creating...");
  dict = [NSMutableDictionary dictionaryWithCapacity:30];

  // create a NSNumber object containing the
  // integer value 0 and add it as 'gameProgress' to the dictionary.
  NSNumber *numberOfCompletedLevels = [NSNumber numberWithInt:0];
  [dict setObject:numberOfCompletedLevels forKey:@"gameProgress"];

  // write dictionary to Documents directory...
  NSLog(@"writing to %@...", plistPath);
  [dict writeToFile:plistPath atomically:YES];
 }
 [self setLevelsCompleted:[[dict objectForKey:@"gameProgress"]intValue]];
 NSLog(@"levels completed is now: %i "), levelsCompleted; 

 // show the contents of the dictionary in the console
 NSLog(@"dictionary values...:");
 for (id key in dict) {
  NSLog(@"key=%开发者_StackOverflow社区@, value=%@", key, [dict objectForKey:key]);
 }

funny thing is, when the code is run, the first NSLog tells me that it sets the "levelsCompleted" variable to 1026944, but when I print the contents of the dictionary, it tells me that the value of "gameProgress" is 0.

clearly something is wrong, and with the intValue not working, i tried with (int) casting, but again unsuccesful.

Does anyone have an idea?

Thank you in advance regards Peter


You wrote: NSLog(@"levels completed is now: %i "), levelsCompleted

The , levelsCompleted should go inside the parentheses. As it is, you're not passing an argument to go with the %i and it's grabbing random garbage that happens to lie where the argument would have been.

Incidentally, the reason this code is valid (though incorrect) is because, outside of an argument list, a comma is an operator. It executes the thing on its left-hand side, discards its value and returns the value of the thing on its right.


Well i have tried your code, and it seems to have no issue,

// create a NSNumber object containing the // integer value 0 and add it as 'gameProgress' to the dictionary. 
NSNumber *numberOfCompletedLevels = [NSNumber numberWithInt:0];
[dict setObject:numberOfCompletedLevels forKey:@"gameProgress"];

int levelsCompleted = [[dict objectForKey:@"gameProgress"]intValue];
NSLog(@"levels completed is now: %i ", levelsCompleted);

// show the contents of the dictionary in the console 
NSLog(@"dictionary values...:"); 
for (id key in dict)
{ 
    NSLog(@"key=%@, value=%@", key, [dict objectForKey:key]); 
}

And the Console output is

2011-01-26 22:30:54.532 Practice[8809:207] dictionary didn't exist, creating...
Current language:  auto; currently objective-c
2011-01-26 22:30:56.187 Practice[8809:207] levels completed is now: 0 
2011-01-26 22:30:56.362 Practice[8809:207] dictionary values...:
2011-01-26 22:30:57.015 Practice[8809:207] key=gameProgress, value=0

May be something is wrong with your class level object "levelsCompleted", check if it is not NSNumber.

But rest looks fine ....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜