views:

79

answers:

1

Hello, I'm working on an Objective-C/Cocoa roguelike, because I've always found that working on a game is the best way to learn about a language. I've gotten quite far with my game, but I'm having a problem.

Each level.plist is defined with a few things, like so:

<key>Save Location</key>
<integer>0</integer>

It's grabbed like so, I'd also like to say that this code is all working fine:

NSObject* level = [mapDictionary_ objectForKey: kLevelNumberKey];

However, in my NSLog-exploring, I've found that instead of grabbing me an integer, or even a useful number, this grabs me an object of some sort, which returns the correct value when NSLogged. How would I go about making this value into an integer so that I can use it like I'd want to be? Note that I've only used NSObject* because nothing else worked right. If I used =(int) instead I would get the pointer value or whatever (1108608 for example).

I'm sure it's a simple solution, and I thank you in advance. If you would not mind explaining exactly what is happening, that would be great. My grasp of the concepts is fine, better than most newcomers, but I'm always interested in exactly what is happening. What use would learning to program be if I didn't know what was going on! Please be more than willing to advise me of other situations like this to watch out/be ready for as well. Any advice, really is appreciated, and will be going to somebody who intends to code for a very long time. I put off learning this much too long, my only regret is not starting earlier.

If, for whatever reason, you are interested in trying out my game so far: Link I'm currently in the process of implementing state-saving so that levels remain as you left them. It's obviously OS X only. I'm pretty proud of it so far, but I have a long ways left. The controls are listed in the app.

+2  A: 

The value is an NSNumber object. You can get it as an int using the -intValue method

Miles
This is exactly what I was looking for, thank you so much. I'm almost disappointed it was such a simple solution. Why is it stored as an NSNumber instead of just an Int?
Sneakyness
Because `int` is a C primitive type. NSDictionaries can only contain objects (likewise, an `id` parameter can only be a pointer to an Objective-C object).
Miles
Container objects (NSArray, NSDictionary, NSSet, etc. In Cocoa only accept objects so you need to wrap your primitives up. That's mainly what NSNumber is for.
Meltemi
So they basically took a better-safe-than-sorry approach to it?
Sneakyness
It has nothing to do with being safe or sorry—primitive types are simply not first-class objects, and can't be referred to with an `id` variable. I suggest you review http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html
Miles
Sneakyness: No. Container objects in Cocoa can only hold other Cocoa objects. `int` isn't a Cocoa object; it's a primitive value. Hence the NSNumber object: since it *is* an object, it can be in a container object.
Peter Hosey