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

tulipano

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
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?
Code:
[COLOR="Lime"]#include <stdio.h>[/COLOR]

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

@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

[COLOR="lime"]#include <stdio.h>[/COLOR]

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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
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.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
so you're saying it should look like this?
Code:
[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

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Code:
#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...
 

Attachments

  • Picture 1.png
    Picture 1.png
    80.7 KB · Views: 70

tulipano

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
This is what shows up on the side bar...I added the Framework "Foundation.framework", but this is the result.
 

Attachments

  • Screen Shot 2012-07-11 at 7.23.39 AM.png
    Screen Shot 2012-07-11 at 7.23.39 AM.png
    80.7 KB · Views: 94

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors newbie
Original poster
Jul 8, 2012
16
0
Boston, MA
Well thank you sir, I will probably be updating this post again with further questions, but until then, thank you all for the help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.