I have been doing java for almost 3 years now, am just starting to learn Objective-C as of this past June.

Been writing a few programs on my own and writing programs for the exercises in 2 very cool Cocoa/objective-c books.

My question is how do I, coming from Java, create a method in @interface? I want to be able to take my java program of 2 years in the making, and move it to objective-c coding. Below is the method in java

public void calculations(double loan, int termYears, double rate, double taxes, double insurance, double downPayment, double addPmt, double squareFeet, double salary)

Now how do I transform this method?

- (void)     calculations: (double) loan  :  (int) termYears  : (double) rate   : (double) taxes   : (double) insurance   : (double) downPayment   : (double) addPmt   : (double) squareFeet   : (double) salary;

Is this correct?

Dani AI

Generated

For : Objective‑C method signatures are idiomatic when each parameter has a descriptive label. Use - for instance methods (use + for class methods), and pass primitives (like double and NSInteger) by value — you don't need * for those. A clear, readable declaration would look like:

- (void)calculationsWithLoan:(double)loan
                   termYears:(NSInteger)termYears
                        rate:(double)rate
                       taxes:(double)taxes
                   insurance:(double)insurance
                 downPayment:(double)downPayment
            additionalPayment:(double)addPmt
                  squareFeet:(double)squareFeet
                      salary:(double)salary;

Put that line in the @interface and implement the same signature in @implementation. To call it:

[self calculationsWithLoan:100000.0
                 termYears:30
                      rate:3.75
                     taxes:150.0
                 insurance:80.0
               downPayment:20000.0
          additionalPayment:0.0
                 squareFeet:1500.0
                     salary:75000.0];

Note on ’s suggestion: using double * makes the parameter a pointer to a double — that’s only appropriate when you intend to modify the caller’s variable (an out parameter) or do low‑level C work. For ordinary numeric inputs, use plain double. If you need object semantics (collections, KVC, nullability) use NSNumber/NSDecimalNumber.

Consider refactoring long parameter lists into a single model object (for example MortgageParameters with properties for loan, term, rate, etc.) or make the method return a computed value (e.g., - (double)monthlyPaymentForLoan:...) — both make code easier to read, test, and maintain.

Recommended Answers

All 3 Replies

You're almost there. But this is the correct one.

(void)     calculations: (double *) loan  :  (int *) termYears  : (double *) rate   : (double *) taxes   : (double *) insurance   : (double *) downPayment   : (double *) addPmt   : (double *) squareFeet   : (double *) salary;

how come you didn't use the - (void)? you just did (void)? It says something in the book about that and I read that + is used too...

oops... sorry missed it by mistake. +/- are about class/instance methods in Objective-c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.