Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > 1 + 1 = 2

1 + 1 = 2
Thread Tools
Fresh-Faced Recruit
Join Date: Mar 2002
Status: Offline
Reply With Quote
Apr 3, 2002, 08:03 AM
 
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
Reply With Quote
Apr 3, 2002, 09:45 AM
 
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
Reply With Quote
Apr 3, 2002, 11:28 AM
 
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
Reply With Quote
Apr 3, 2002, 09:33 PM
 
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).
Rick Roe
icons.cx | weblog
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:31 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2