Objective C Help

Discussion in 'Mac Programming' started by nashyo, Dec 5, 2011.

  1. nashyo, Dec 5, 2011
    Last edited by a moderator: Dec 5, 2011

    macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I'm getting an error from Xcode 4.2.1 'Sending 'double' to parameter of incompatible type 'NSNumber'.
    Please help.

    the error occurs at the following (which can be found in position below)

    for (NSNumber *aTransaction in europeTransactions) {
    [europeBudget spendDollars: [aTransaction doubleValue]];
    }​


    The code goes like this...
    Code:
    #import <Foundation/Foundation.h>
    
    @interface 
    
    Budget : NSObject 
    
    {
        float exchangeRate;
        double budget;
        double exchangeTransaction;
    }
    
    - (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
    - (void) spendDollars: (NSNumber*) dollars;
    - (void) chargeForeignCurrency: (double) foreignCurrency;
    
    @end
    
    @implementation 
    
    Budget
    - (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate {
        budget = aBudget;
        exchangeRate = anExchangeRate;        
    }
    
    - (void) spendDollars: (NSNumber*) dollars {
        budget -= [dollars doubleValue];
        NSLog(@"Converting %.2f US dollars into foreign currency leaves $%.2f", [dollars doubleValue], budget);
    }
    
    - (void) chargeForeignCurrency: (double) foreignCurrency {
        exchangeTransaction = foreignCurrency*exchangeRate;
        budget -= exchangeTransaction;
        NSLog(@"Charging %.2f in foreign currency leaves $%.2f", foreignCurrency, budget);
    }
    
    @end
    
    int main (int argc, const char * argv[]) {    
        
    double numberEuros = 100;
        
    NSNumber *europeDollarTransaction = [[NSNumber alloc] initWithDouble:100];
    NSNumber *europeDollarTransaction2 = [[NSNumber alloc] initWithDouble:200];
                                              
    NSMutableArray *europeTransactions = [[NSMutableArray alloc] initWithCapacity:1];
    
    [europeTransactions addObject:europeDollarTransaction];
    [europeTransactions addObject:europeDollarTransaction2];
                                              
    Budget *europeBudget = [Budget new];
                                              
    [europeBudget createBudget:1000.00 withExchangeRate:1.2500];
        
    for (NSNumber *aTransaction in europeTransactions) {
    [europeBudget spendDollars: [aTransaction doubleValue]];
    }
        
    [europeBudget chargeForeignCurrency:numberEuros];
         
    return 0;
    }
     
  2. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #2
    I can't read your code (use the code tags to post code snippets), but if you don't send the doubleValue message to your aTransaction variable it'll probably work. It sounds like the spendDollars method expects an NSNumber, and you are sending it a double.
     
  3. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Is this better?

    Thanks for adding the codes snippets for me.

    The message I'm getting from Xcode is pretty much exactly what you have said @mobilehaathi. I just don't understand how I can send it an NSNumber, instead of a double...That's confusing.

    My NSNumber is a double.

    I'm just starting out.
     
  4. macrumors member

    Joined:
    Aug 8, 2011
    #4
    Passing NSNumber numberWithDouble: [your_double_here] should work, I think.
     
  5. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #5
    Change your for loop to:

    Code:
    for (NSNumber *aTransaction in europeTransactions) {
         [europeBudget spendDollars:aTransaction];
    }
    
    NSNumber is an Objective-C class, so variables of type NSNumber point to objects. On the other hand, double is a C-style primitive type. A variable of type double contains is a simple value, not an object. An NSNumber can be used to store a value that starts out as a double, but it can't be said to be a double. Your -spendDollars: method takes an NSNumber as its argument, but you're passing it a variable of type double.

    Alternatively, you could change your -spendDollars method so that it takes a double instead of an NSNumber. Which you choose to do is up to you. In this simple case, it probably doesn't make much of a difference. If that's how you want to go:

    Code:
    
    - (void)spendDollars:(double)dollars {
        budget -= dollars;
        NSLog(@"Converting %.2f US dollars into foreign currency leaves $%.2f", dollars, budget);
    }
    
    
     
  6. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #6
    Right, sending a doubleValue message to an instance of NSNumber will return a double typed result. Your
    Code:
    [aTransaction doubleValue]
    is effectively typed as a double, yet your method is expecting an NSNumber type.
     
  7. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    Much appreciated

    Thank you so much!

    I'm using 'Objective C for dummies' and I literally just copied the code in, as it is written in the book. The book is wrong!

    My code works now, thanks to this thread. Thanks everyone. I was losing my rag!

    Kind Regards
     
  8. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #8
    Great! Although the most important thing is that you understand why it was wrong. If you understand why it didn't work and why what you changed fixed it, then you're learning.
     
  9. macrumors 603

    Joined:
    Aug 9, 2009
    #9
    Google search terms: Objective C for dummies

    The book's website appears in the top few results. Go there and download the code, read the cheat sheet, look for corrections, etc.

    No computer-language book in recent years is published without a companion website. That website almost always has downloadable code, and usually contains corrections (called "errata") for the book's text and code.
     
  10. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #10
    Thanks. This is the first book I've used so far, so this is a really helpful point.
     

Share This Page