-1

I am an Obj-C newbie so this might be an obvious question.

I am adding 10 numbers as keys to an NSMutableDictionary with value 0 for all of them. Something like this --

for( i=1; i<10; i++)
{
    [dict setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:i]];
}

Now at a later point in code I want to get the object stored for the key number and obtain its value. In this case for e.g. for number 3 as key we stored the value 0. For this I use this code -

id numberObj = [game objectForKey:[NSNumber numberWithInt:3]];
int numberObjValue = ????

I see id in the debugger as pointing to 0. But I am not sure how to get it into an integer variable I declared. Hope that makes sense.

I print numberObj and some address is printed as expected. How do I turn this generic id into a NSNumber and subsequently store it in numberObjValue?

user220201
  • 4,514
  • 6
  • 49
  • 69
  • You are not using the correct class for storage. Use an array, not a dictionary. – duci9y Jul 27 '14 at 07:28
  • You are right, I don't need a dictionary for this specific example. But I simplified my problem for the sake of ease of explanation. The main problem I have is converting the generic id obect into an object I actually inserted into the Dictionary. How do I do it? – user220201 Jul 27 '14 at 07:31
  • Look at the docs for `NSNumber`. You'd find `integerValue` and other related methods there. Please read the docs before posting here. – duci9y Jul 27 '14 at 07:33
  • Its not that I don't know how to convert NSNumber into an int. Please see my comment above. – user220201 Jul 27 '14 at 07:35
  • 2
    @Please see your post above. Your code makes it appear that you're trying to convert the object into an `int`. Also, do you have enough knowledge about C before you start learning Objective-C? Read up on pointers and casting. – duci9y Jul 27 '14 at 07:38

3 Answers3

0

You can get the number from the dictionary and then store it in an int using intValue:

NSNumber *numberObj = (NSNumber *)[game objectForKey:[NSNumber numberWithInt:3]];
int numberObjValue = [numberObj intValue];

Or, you can do it with one line:

int numberObjValue = [[game objectForKey:[NSNumber numberWithInt:3]] intValue];
rebello95
  • 8,486
  • 5
  • 44
  • 65
0

id is not a datatype. It simple means it can return any object type. When you are sure you have inserted an NSNumber, you simply get it's value into an NSNumber.

NSNumber* myNumber = [myDict objectForKey:key];

That's it. If you want to make sure it is actually an NSNumber being returned, use isKindOfClass:.

n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
  • @downvoter: why did you down vote. please leave a comment. I think this in fact the best answer here as it is the only one at least trying (briefly) to explain what id is and how it works. – Daij-Djan Jul 27 '14 at 07:41
  • I will add more code, in a while. Also, downvoters don't matter when I know my code is correct. :) – n00bProgrammer Jul 27 '14 at 07:42
  • I didn't downvote your answer but opening with "`id` is not a datatype" is what did it I suspect. – trojanfoe Jul 27 '14 at 07:47
  • Maybe not the best start but it does make sense:: see e.g. http://stackoverflow.com/questions/1304176/objective-c-difference-between-id-and-void – Daij-Djan Jul 27 '14 at 07:50
  • @duci9y: `id` is a type, but it isn't really a *data* type. It's just a particular kind of pointer that allows messages without typechecking. It's kind of a confusing way to phrase it, but I think it is a useful distinction to have. – Chuck Jul 27 '14 at 07:51
  • @Daij-Djan Doesn't make sense at all. `id` is a type. `void *` is a type. So would `id ******` be a valid type. – duci9y Jul 27 '14 at 07:51
  • In all fairness, it was the downvote which brought to my attention the fact, I had not answered the OP. – n00bProgrammer Jul 27 '14 at 07:52
  • god, normally Im the picky one. ID is a typedef, so yes in effect it is a datatype but actually it is only a pointer to something .... as is also written in the answer I linked to – Daij-Djan Jul 27 '14 at 07:53
  • @Chuck I'd suppose 'type' and 'data type' mean the same thing. – duci9y Jul 27 '14 at 07:53
  • I wish I weren't in office on a Sunday. I would love to respond. – n00bProgrammer Jul 27 '14 at 07:55
  • 1
    @duci9y: There are important differences both in theory and in practice between references like pointers and value types like ints and structs. They are all types, but they aren't the same thing. – Chuck Jul 27 '14 at 07:59
  • @Chuck Within the context of this question, OP's statement that `id` is not a data type is wrong. And I never said anything about there not being differences between ints, structs or pointers. I'm just saying all of them are types. – duci9y Jul 27 '14 at 08:00
  • This reminds me of [Cunningham's Law](http://meta.wikimedia.org/wiki/Cunningham's_Law) – n00bProgrammer Jul 27 '14 at 13:24
0

The object is already an NSNumber. You put an NSNumber in, and you got an NSNumber out. There is no conversion necessary. You can assign it to an NSNumber variable, but the object would not be any more or less an NSNumber for having done so. So you don't need to turn it into an NSNumber, and you can get the number's integer value in the usual way you do with all NSNumbers.

Chuck
  • 234,037
  • 30
  • 302
  • 389