PDA

View Full Version : Objective C Help




nashyo
Dec 5, 2011, 07:55 AM
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...

#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;
}



mobilehaathi
Dec 5, 2011, 08:00 AM
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.

nashyo
Dec 5, 2011, 10:00 AM
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.

iHutch105
Dec 5, 2011, 10:10 AM
Passing NSNumber numberWithDouble: [your_double_here] should work, I think.

mduser63
Dec 5, 2011, 10:14 AM
Change your for loop to:


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:



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

mobilehaathi
Dec 5, 2011, 10:37 AM
Right, sending a doubleValue message to an instance of NSNumber will return a double typed result. Your [aTransaction doubleValue] is effectively typed as a double, yet your method is expecting an NSNumber type.

nashyo
Dec 7, 2011, 11:26 AM
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

mobilehaathi
Dec 7, 2011, 11:37 AM
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.

chown33
Dec 7, 2011, 11:40 AM
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.

nashyo
Dec 7, 2011, 11:47 AM
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.