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

rickb

macrumors newbie
Original poster
Dec 5, 2006
12
0
I'm getting an error in currency converter, while I'm sure I typed something wrong somewhere I want to understand my problem better

my ConverterController.m is:

#import "ConverterController.h"

Code:
@implementation ConverterController

- (IBAction)convert:(id)sender
{
	float rate, currency, amount;
	currency = [dollarField floatValue];
	rate = [rateField floatValue];
	amount = 8;//
	id temp = [converter convertCurrency:currency atRate:rate];
	[amountField setFloatValue:amount];
	[rateField selectText:self];
}

@end


The problem was that I was getting an error on setting the amount field, in the debugger I realized that

[converter convertCurrency:currency atRate:rate];

was returning a pointer, not a float? Which is why I can see it works if I explicitly create a float instead of a pointer to a float. Also would love to know what I did wrong.

My Converter.m is:
Code:
@implementation Converter
- (float)convertCurrency:(float)currency atRate:(float)rate {
	float total = currency * rate;
	return total;
}
I am fluent with java but I'm not sure I understand how the demo was supposed to originally work if the setAmount field expects a float but gets a pointer. Then again in Java, pointers/types are much simpler.

Also, this is an example of messaging right, instead of explicit methods? It seems for a simple demo messaging would be more inherent to tough to spot problems.

thanks
 

rickb

macrumors newbie
Original poster
Dec 5, 2006
12
0
Enclosed...
 

Attachments

  • CurrencyConveter.zip
    41.4 KB · Views: 94

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Fixed a few warnings in your project.

Converter.h should look like this:

Code:
/* Converter */

#import <Cocoa/Cocoa.h>

@interface Converter : NSObject
{
}

- (float)convertCurrency:(float)currency atRate:(float)rate;

@end

The method declaration was a bit off, although it shouldn't have made a difference.

And then reverting convert: back to its original state:

Code:
- (IBAction)convert:(id)sender
{
	float rate, currency, amount;
	currency = [dollarField floatValue];
	rate = [rateField floatValue];
	amount = [converter convertCurrency:currency atRate:rate];
	[amountField setFloatValue:amount];
	[rateField selectText:self];
}

Everything works fine.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Why do you have
Code:
id temp = [converter convertCurrency:currency atRate:rate];
instead of
Code:
float temp = [converter convertCurrency:currency atRate:rate];
?

Your method is returning a float, but you're the one converting it to a pointer!
 

rickb

macrumors newbie
Original poster
Dec 5, 2006
12
0
Thanks! aaaaa, makes sense...sort of.

How would I write the call to converter explicitly without the messaging paradign?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Thanks! aaaaa, makes sense...sort of.

How would I write the call to converter explicitly without the messaging paradign?

You wouldn't. There are ways to call into the runtime directly to pass the message,but this is OO programming, we send messages.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.