Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 5, 2011, 07:55 AM   #1
nashyo
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Objective C Help

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 maflynn; Dec 5, 2011 at 09:57 AM. Reason: Added code tags
nashyo is offline   0 Reply With Quote
Old Dec 5, 2011, 08:00 AM   #2
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: The Anthropocene
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.
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Dec 5, 2011, 10:00 AM   #3
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
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.
nashyo is offline   0 Reply With Quote
Old Dec 5, 2011, 10:10 AM   #4
iHutch105
macrumors member
 
Join Date: Aug 2011
Passing NSNumber numberWithDouble: [your_double_here] should work, I think.
__________________
2011 iMac 27" | 2009 MacBook 13" | iPhone 4 16GB Black
iHutch105 is offline   0 Reply With Quote
Old Dec 5, 2011, 10:14 AM   #5
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
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);
}
mduser63 is offline   1 Reply With Quote
Old Dec 5, 2011, 10:37 AM   #6
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: The Anthropocene
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.
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Dec 7, 2011, 11:26 AM   #7
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
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
nashyo is offline   0 Reply With Quote
Old Dec 7, 2011, 11:37 AM   #8
mobilehaathi
macrumors 601
 
mobilehaathi's Avatar
 
Join Date: Aug 2008
Location: The Anthropocene
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.
__________________
The true way is along a rope that is not spanned high in the air, but only just above the ground. It seems intended more to cause stumbling than to be walked along.
mobilehaathi is offline   0 Reply With Quote
Old Dec 7, 2011, 11:40 AM   #9
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by nashyo View Post
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.
chown33 is offline   1 Reply With Quote
Old Dec 7, 2011, 11:47 AM   #10
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Quote:
Originally Posted by chown33 View Post
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.
nashyo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
nsnumber, objective c

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
I Need Help Learning Objective-c!! Woodshark69 Mac Programming 11 Dec 14, 2012 07:21 AM
Objective C Accessors ag2web Mac Programming 9 Nov 18, 2012 02:53 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM
Testing objective-c Evre iPhone/iPad Programming 21 Jul 12, 2012 07:22 AM
Objective C Syntax nashyo iPhone/iPad Programming 6 Jul 4, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 11:24 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC