Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Taco Bell

macrumors newbie
Original poster
Mar 4, 2010
2
0
I'm fairly experienced in ANSI C. I actively program microcontrollers for robots. This requires an immense amount of mathematical operation, as does most programming. But Cocoa doesn't seem to support even the most simple mathematical operations. So, can someone tell me how to perform mathematical operations in Cocoa?

I have tried to make an application that adds two number together. I made an interface that that consists of three text fields, two for the numbers being added, one for the sum, and a button to initiate the calculation. But that as far as I got.
 
Code:
5 + 5

like so?

To be less snarky, Cocoa is an API. The language is Objective-C which is a strict superset of C. You can use all the C operators you're familiar with.
 
How do you cast a number form a NSTextField to an integer?

As Lee pointed out, you can just use the text field's intValue: method (inherited from NSControl):

Code:
int i = [myTextField intValue];

Or you might want to get the value as an NSString first using stringValue: to do some formatting and error-checking on it (or place an NSNumberFormatter on the field), then convert the value using NSString's intValue: method.
 
I'm fairly experienced in ANSI C. I actively program microcontrollers for robots. This requires an immense amount of mathematical operation, as does most programming. But Cocoa doesn't seem to support even the most simple mathematical operations. So, can someone tell me how to perform mathematical operations in Cocoa?
Using C types and C arithmetic.

I have tried to make an application that adds two number together. I made an interface that that consists of three text fields, two for the numbers being added, one for the sum, and a button to initiate the calculation. But that as far as I got.

If you're not working from a tutorial or guided instruction of some kind, making up your own is almost certainly going to fail.

See Apple's introductory tutorial:
http://developer.apple.com/mac/libr...jCTutorial/01Introduction/01Introduction.html

If your example of an application that adds two numbers together is from a tutorial, please post its URL. It may expect a prerequisite.
 
But when it comes to medium level things, like exponents and trig, I still have no idea how to do it. Luckily I haven't had to yet.

They're done exactly the same way they're done in standard C: with functions in the math library.

exponentiation is the pow() function.

trig functions: sin(), cos(), atan2(), etc.

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/math.3.html

A local (non-web) version of the man pages is available using Xcode; there's a man page menu-item under its Help menu.

Finally, I find this program useful for reading man pages:
http://www.bruji.com/bwana/

It shows man pages in your browser using a man: url scheme. It even responds to the command-line 'open man:whatever' by opening the man page in the browser.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.