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

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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;
}
 
Last edited by a moderator:
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.
 
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.
 
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);
}
 
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.
 
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
 
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.
 
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!

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.
 
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.

Thanks. This is the first book I've used so far, so this is a really helpful point.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.