Re: Yet another stupid newbie's question
Re: Yet another stupid newbie's question
- Subject: Re: Yet another stupid newbie's question
- From: Simon Stapleton <email@hidden>
- Date: Tue, 28 Aug 2001 13:59:17 +0100 (BST)
>
Subject: Yet another stupid newbie's question
>
From: "email@hidden" <email@hidden>
>
To: Unbenannt <email@hidden>
>
>
Hello list,
>
>
It's again around type conflicts, pointers and stuff... Probably I'm
>
trying
>
to mix objects and c-style vars in a wrong way
>
>
I've done an array containing integers.
>
it's filled by:
>
...
>
[myArray addObject:[NSNumber numberWithInt:10]];
>
[myArray addObject:[NSNumber numberWithInt:123]];
>
[myArray addObject:[NSNumber numberWithInt:4]];
>
[myArray addObject:[NSNumber numberWithInt:56]];
>
...
>
>
A simple
>
>
for (index=0;index<max;index++) {
>
NSLog (@"debug --> %@", [myArray objectAtIndex:index]);
>
}
>
>
tells me that all values are stored fine.
>
>
Well, my question is, how can I do some mathematics (additions,
>
comparing in
>
size) with this values???
>
>
int value=[myArray objectAtIndex:0] makes the compiler scream:
>
"assignment makes integer from pointer without a cast"... :-(
>
>
TIA,
>
Martin
The problem here is that the objects in the array are of type
NSNumber *, so trying to assign them to an int will, of course, cause
the compiler to go doolally.
To get your numbers back into (for example) ints, you'll want
int anInteger = [[myArray objectAtIndex:0] intValue];
for other types, you'll want one of the following:
- boolValue
- charValue
- decimalValue
- descriptionWithLocale:
- doubleValue
- floatValue
- intValue
- longLongValue
- longValue
- shortValue
- stringValue
- unsignedCharValue
- unsignedIntValue
- unsignedLongLongValue
- unsignedLongValue
- unsignedShortValue
You can also compare NSNumber values, but there are no math
primitives defined as far as I can tell.
I suppose you could create a category to do math with NSNumbers
directly, but it would likely create a bit of an overhead.
Simon
--
If the answer isn't obvious, the question is a distraction. Go find
an
easier question.