 |
 |
1 + 1 = 2
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2002
Status:
Offline
|
|
How would i do simple calculations like this in Objective-C?
I have 2 NSNumber Objects containing number values and i want to add these in another field.
I guess i probably have to convert NSNumbers to int, but how?
Thanks in advance,
Cocoala
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
Apple's Documentation on NSNumber
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSNumber *a,*b, *c;
a = [NSNumber alloc];
b = [NSNumber alloc];
c = [NSNumber alloc];
[a initWithInt: <font color = blue>1</font>];
[b initWithInt: <font color = blue>1</font>];
[c initWithInt: [a intValue] + [b intValue]];
</font>[/code]
That would technically be how you would do what you requested purely with NSNumbers. you could also use the +numberWithInt: function on the NSNumber class instead of using alloc and init seperately:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
a = [NSNumber numberWithInt: <font color = blue>1</font>];
</font>[/code]
That developer documentation comes in really handy sometimes. But in Objective-C, there is a much much easier way to do what you requested:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
int a=<font color = blue>1</font>,b=<font color = blue>1</font>,c;
c=a+b;
</font>[/code]
don't bother with NSNumbers to do that kind of stuff.
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
I second the basic advice -- it's usually easier to work with scalar ints, floats, etc. NSNumber is useful as a wrapper when you need objects (to, say, put in an NSDictionary or something) but otherwise they're a bit of a pain. You'll notice that almost none of the AppKit APIs use NSNumbers -- they use unsigned, float, double, etc.
One note though on the example code -- don't split up the +alloc and -init calls like that. -init* methods are allowed to return different objects than the original receiver. NSNumber is a class cluster, so it's one example of where this happens -- it returns a specific subclass based on how you initialize it.
I.e do NOT do:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSNumber *a;
a = [NSNumber alloc];
[a initWithInt: <font color = blue>1</font>];
</font>[/code]
At the very least do:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSNumber *a;
a = [NSNumber alloc];
a = [a initWithInt: <font color = blue>1</font>];
</font>[/code]
or better:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSNumber *a;
a = [[NSNumber alloc] initWithInt:<font color = blue>1</font>];
</font>[/code];
As you say, [NSNumber numberWithInt:1] is also easy, and it is already autoreleased that way. When you use +alloc, you have to call either -release or -autorelease on it yourself.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
You don't want to do math with NSNumbers... just do it with plain C operators and primitive data types, unless you need something (like arbitrary precision or complex math) that C doesn't provide. NSNumbers are for the purpose of wrapping primitive-type values in objects for use with other code that expects to be dealing with objects (like storing them in an NSArray).
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|