PDA

View Full Version : Help with @interface code




tulipano
Jul 9, 2012, 06:15 PM
I am a beginner in objective-c coding, so please take it easy on me. Xcode is saying I have an error, in red, saying it expected ( or ) identifiers...any solutions? Also, is it necessary to have both of the items bolded in green? #include <stdio.h>

//Class Name and it's superclass (Budget, NSObject respectively)
//The inherits all of the superclass's methods as well as the ones in code
@interface Budget : NSObject {
//Instance Variables: declaration of variables
float exchangeRate;
double budget;
double exchangeTransaction;
}
//Methods: the function of the class
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) chargeForeignCurrency: (double) foreignCurrency;

@end

@implementation Budget
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate
{
exchangeRate = anExchangeRate;
budget = aBudget;
}

- (void) spendDollars: (double) dollars {
budget -= dollars;
printf("Converting %.2f US dollars into foreign currency leaves $%.2f", dollars, budget);
}
- (void) chargeForeignCurrency: (double) foreignCurrency {
exchangeTransaction = foreignCurrency * exchangeRate;
budget -= exchangeTransaction;
printf("Charging %2f in foreign currency leaves $%.2f", foreignCurrency, budget);
}

@end

#include <stdio.h>

int main(int argc, const char * argv[])
{
double numberDollarsEurope = 100;
double numberEuros = 100;
double numberDollarsEngland = 100;
double numberPounds = 100;

Budget *europeBudget = [Budget new];

[europeBudget createBudget: 1000.00 withExchangeRate 1.2500];
[europeBudget spendDollars:numberDollarsEurope];
[europeBudget chargeForeignCurrency:numberEuros];

Budget *englandBudget = [Budget new];

[englandBudget createBudget: 2000.00 withExchangeRate 1.5000];
[englandBudget spendDollars: numberDollarsEngland];
[englandBudget chargeForeignCurrency:numberPounds];


return 0;
}


----------

This is also coming out of a book, so any reason to why I am doing what I am doing I cannot answer for this is the code the author wanted me to write.



lee1210
Jul 10, 2012, 01:28 AM
You need to #import <Foundation/Foundation.h> to resolve the NSObject issue. When you pass the createBudget:withExchangeRate: message, you missed the : between withExchangeRate and the argument both times you called it.

Is this all in one file? If so, one include of stdio.h should be fine. Traditionally #import is used instead, though.

I fixed these things and the code compiled and ran. I didn't check it for correctness, just getting it to compile. There aren't any newlines in there, so I got one big line of output.

-Lee

tulipano
Jul 10, 2012, 08:36 AM
Thanks lee. Should I always use the #import <Foundation.h>? And how can I separate the outcome so it shows different lines? So each individual printf() shows on a separate line.

balamw
Jul 10, 2012, 08:40 AM
This is also coming out of a book, so any reason to why I am doing what I am doing I cannot answer for this is the code the author wanted me to write.

It helps to tell us which book. Is this it: Objective-C For Dummies (http://www.amazon.com/Objective-C-Dummies-Computers-Neal-Goldstein/dp/0470522755)?

B

whooleytoo
Jul 10, 2012, 08:47 AM
Thanks lee. Should I always use the #import <Foundation.h>?

If you're using Cocoa classes (such as NSObject), then yes. Depending on how you created the project, you probably need to add the Foundation framework to your project. (Select the Project in the left-hand pane in Xcode, then select the Target in the right-hand pane. Select the "Summary" tab. You can add the framework in the "Linked Frameworks and Libraries" list below)

If you expand the Foundation framework, you can see the headers of the various classes included. If you need to use any of those classes, you need that framework.

tulipano
Jul 10, 2012, 10:16 AM
Yes, it is objective-c for dummies. And thanks ^

tulipano
Jul 10, 2012, 05:41 PM
Lee: I don't understand what you are saying when you said I am missing a colon in between the withExchangeRate and the argument...

Whooley: should I use both frameworks, or just the foundation.h?

whooleytoo
Jul 10, 2012, 05:50 PM
Lee: I don't understand what you are saying when you said I am missing a colon in between the withExchangeRate and the argument...

Whooley: should I use both frameworks, or just the foundation.h?

You need to both add the framework and #import the header file.

If your project is using any Foundations classes (NSObject, NSString, NSArray etc) you need to include the Foundation framework. Then in whichever source file uses that class, you #import the Foundation.h file. Typically, if you create a project using one of the Xcode OS X/iOS templates, this will already be done for you.

(Oh, and the issue lee1210 noticed is in the line below, and the other line like it. You're missing a colon after withExchangeRate)
[europeBudget createBudget: 1000.00 withExchangeRate 1.2500];

tulipano
Jul 10, 2012, 07:10 PM
so you're saying it should look like this?[europeBudget creatBudget: 1000.00 withExchangeRate: 1.2500];


also, I added the framework and I hit build and run and got 20 errors....this is stressful

balamw
Jul 10, 2012, 07:52 PM
Can you post the entire code and the warnings/errors you are getting. FWIW the excerpts from the book I can see via Amazon/Google Books use NSLog instead of printf. This would add the line separators you were missing.

B

kryten2
Jul 10, 2012, 08:47 PM
#import <Foundation/Foundation.h>
#include <stdio.h>
// insert code here...
NSLog(@"Converting %.2f US dollars into foreign currency leaves $%.2f", dollars, budget);
// insert code here...
NSLog(@"Charging %2f in foreign currency leaves $%.2f", foreignCurrency, budget);
// insert code here...
[europeBudget createBudget: 1000.00 withExchangeRate: 1.2500];
// insert code here...
[englandBudget createBudget: 2000.00 withExchangeRate: 1.5000];
// insert code here...

tulipano
Jul 11, 2012, 06:26 AM
This is what shows up on the side bar...I added the Framework "Foundation.framework", but this is the result.

whooleytoo
Jul 11, 2012, 06:35 AM
If you're just starting, the easiest approach might be to create a new project and copy your code in.

If so: create a new Xcode project, select "Command Line Tool", then in the following drop-down menu, select "Foundation". You should be able to add your code then into main.m.

tulipano
Jul 11, 2012, 07:18 AM
you are a genius my friend. worked perfectly. Now would I usually keep the drop down at "C" or "Foundation"

whooleytoo
Jul 11, 2012, 08:37 AM
you are a genius my friend. worked perfectly. Now would I usually keep the drop down at "C" or "Foundation"

That depends on what code you want to write. Choose C if you're using C only; C++ if you're using C++ and/or C; and Foundation if you're using Objective-C and/or C, and need to use Foundation classes.

If you eventually progress onto developing Mac/iOS apps, you'll choose one of the application project templates instead, which will include Foundation, and also include UIKit (iOS) or AppKit(OS X) which include the higher-level classes you might need, such as windows, views etc.

tulipano
Jul 11, 2012, 10:08 AM
Well thank you sir, I will probably be updating this post again with further questions, but until then, thank you all for the help