Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2011, 08:34 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
CSaks is on a distinguished road
Default What am I doing wrong here? (really noobish problem))

Hi, I've been attempting to learn Objective-C for around a month now and at the moment I am trying to make a simple vacaction money spending program.

I am trying to implement NSNumber & NSMutableArray into the program and I have run across an error when i go to build and run:

Code:
NSNumber *europeDollarTransaction = [[NSNumber alloc] initWithDouble:100.00];
	NSNumber *europeDollarTransaction2 = [[NSNumber alloc] initWithDouble:200.00];
	
	NSMutableArray *europeTransactions = [[NSMutableArray alloc] initWithCapacity:1];
	[europeTransactions addObject:europeDollarTransaction];
	[europeTransactions addObject:europeDollarTransaction2];
	
	
	Budget *europeBudget = [Budget new];
	[europeBudget createBudget: 1000.0 withExchangeRate: 1.2500];
	for (NSNumber *aTransaction in europeTransactions) {
		[europeBudget spendDollars: [aTransaction doubleValue]];
	}
	[europeBudget chargeForeignCurrency:numberEuros];
The error states "Incompatible type for argument 1 of spendDollars" after this line of code
Code:
 [europeBudget spendDollars: [aTransaction doubleValue]];
Can someone explain what i've done wrong? Ill admit this is a really noobish question so sorry!

Also I have checked to see if I have misspelled anything/missed out simicolon..etc. but can't see anything

Thanks
CSaks is offline   Reply With Quote
Old 12-05-2011, 11:26 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 280
dapis is on a distinguished road
Default

Quote:
Originally Posted by CSaks View Post
Hi, I've been attempting to learn Objective-C for around a month now and at the moment I am trying to make a simple vacaction money spending program.

I am trying to implement NSNumber & NSMutableArray into the program and I have run across an error when i go to build and run:

Code:
NSNumber *europeDollarTransaction = [[NSNumber alloc] initWithDouble:100.00];
	NSNumber *europeDollarTransaction2 = [[NSNumber alloc] initWithDouble:200.00];
	
	NSMutableArray *europeTransactions = [[NSMutableArray alloc] initWithCapacity:1];
	[europeTransactions addObject:europeDollarTransaction];
	[europeTransactions addObject:europeDollarTransaction2];
	
	
	Budget *europeBudget = [Budget new];
	[europeBudget createBudget: 1000.0 withExchangeRate: 1.2500];
	for (NSNumber *aTransaction in europeTransactions) {
		[europeBudget spendDollars: [aTransaction doubleValue]];
	}
	[europeBudget chargeForeignCurrency:numberEuros];
The error states "Incompatible type for argument 1 of spendDollars" after this line of code
Code:
 [europeBudget spendDollars: [aTransaction doubleValue]];
Can someone explain what i've done wrong? Ill admit this is a really noobish question so sorry!

Also I have checked to see if I have misspelled anything/missed out simicolon..etc. but can't see anything

Thanks
How is the method spendDollars declared?
Does it take a double value?
The error message would suggest that it's not declared to take a double as a parameter.
dapis is offline   Reply With Quote
Old 12-06-2011, 01:46 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
CSaks is on a distinguished road
Default

Quote:
Originally Posted by dapis View Post
How is the method spendDollars declared?
Does it take a double value?
The error message would suggest that it's not declared to take a double as a parameter.
Hi, here is my interface;
Code:
@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
And implementation for spendDollars;

Code:
- (void) spendDollars:(NSNumber*)dollars {
	budget -= [dollars doubleValue];
	NSLog(@"Coverting %.2f dollars into foreign currency leaves $%.2f", [dollars doubleValue], budget);
}
And yes I think you might be right, what do you suggest?

Thanks for helping.
CSaks is offline   Reply With Quote
Old 12-06-2011, 01:48 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

NSMutableArray's are really meant to hold strings and not numbers, so you might have better luck if you store the numbers as strings and then convert them back to numbers when you use them.

also you are declaring the array as initWithCapacity:1, but then adding 2 values. not sure but you might be better off setting the capacity higher or leaving it off all-together. try this:

NSMutableArray *europeTransactions = [[NSMutableArray alloc] init];

[europeTransactions addObject:[NSString stringWithFormat:@"%d", europeDollarTransaction]];
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 12-06-2011, 01:54 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

I think i see the problem:

you function is looking for NSNumber:

- (void) spendDollarsNSNumber*)dollars

but you are trying to pass in a doubleValue

try using float for everything to keep things consistent (and be sure to leave off the asterisk!):

Code:
- (void) spendDollars:(float)dollars {
Code:
[europeBudget spendDollars: [aTransaction floatValue]];
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 12-06-2011, 07:53 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
CSaks is on a distinguished road
Default

Hi guys, thanks for all the help, after hours I have finally managed to work out what the problem was.

If you are interested; In the implementation I had already specified that it was a doubleValue:
Code:
- (void) spendDollars:(NSNumber*)dollars {
	budget -= [dollars doubleValue];
	NSLog(@"Coverting %.2f dollars into foreign currency leaves $%.2f", [dollars doubleValue], budget);
}
So in this line of code I didn't need to specify that it was doubleValue:
Code:
[europeBudget spendDollars: [aTransaction doubleValue]];
So instead I just put:
Code:
[europeBudget spendDollars: aTransaction];
Thanks for all the help! Im sure I'll be back again soon enough needing more with another problem
CSaks is offline   Reply With Quote
Old 12-07-2011, 04:56 AM   #7 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
NSMutableArray's are really meant to hold strings and not numbers, so you might have better luck if you store the numbers as strings and then convert them back to numbers when you use them.
That's wrong on multiple levels. NSArrays are meant to hold objects of any kind. They don't care what objects and they don't have to be the same kind. Holding numbers as strings is also a bad idea. In many cases you can lose precision and in most cases you will use more memory and processing power by converting from one to the other all the time.
baja_yu is offline   Reply With Quote
Old 03-28-2012, 12:31 AM   #8 (permalink)
Registered Member
 
michal26's Avatar
 
Join Date: Mar 2012
Posts: 10
michal26 is on a distinguished road
Default

I think you are passing through doubleValue you should try using float and it might solve your problem.
__________________
PayPal
michal26 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 494
14 members and 480 guests
AlanFloyd, andyzaharia, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, mutantskin, Paul Slocum, tiendung2992, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,684
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tiendung2992
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0