Help with @interface code

Discussion in 'Mac Programming' started by tulipano, Jul 9, 2012.

  1. macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #1
    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.
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #3
    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.
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    It helps to tell us which book. Is this it: Objective-C For Dummies?

    B
     
  5. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #6
    Yes, it is objective-c for dummies. And thanks ^
     
  7. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #7
    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?
     
  8. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #8
    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];
     
  9. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #9
    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
     
  10. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    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
     
  11. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    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...
     

    Attached Files:

  12. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #12
    This is what shows up on the side bar...I added the Framework "Foundation.framework", but this is the result.
     

    Attached Files:

  13. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #13
    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.
     
  14. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #14
    you are a genius my friend. worked perfectly. Now would I usually keep the drop down at "C" or "Foundation"
     
  15. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #15
    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.
     
  16. thread starter macrumors newbie

    Joined:
    Jul 8, 2012
    Location:
    Boston, MA
    #16
    Well thank you sir, I will probably be updating this post again with further questions, but until then, thank you all for the help
     

Share This Page