Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > CurrencyConverter Tutorial

CurrencyConverter Tutorial
Thread Tools
Mac Enthusiast
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 21, 2001, 04:59 PM
 
before about a week ago, the only programming experience I ever had was high school pascal, so I suppose I should warn all that this is a severe newbie question...

anyway, I wanted to get into cocoa development, seeing as how Apple had included all the developer tools in the box with X and whatnot... so I fired up CurrencyConverterTutorial.pdf and got to work.

I followed the tutorial verbatim (as far as I can tell -- I've checked over it a hundred times looking for what went wrong...) and now, whenever I debug/run a build of CurrencyConverter, it behaves in a way I don't understand.

The app has three fields, you know, being rateField, dollarField, and totalField. you enter your values in rateField and dollarField, which are then fed into ConverterController.m:

...
- (IBAction)convertid)sender
{
float rate, amt, total;

amt = [dollarField floatValue];
rate = [rateField floatValue];

total = [converter convertAmount:amt atRate:rate];

[totalField setFloatValue:total];
[rateField selectText:self];
}
...


and then, when the "convert" button is activated in the app, convert: is called:

...
- (float)convertAmountfloat)amt atRatefloat)rate;
...


in Interface Builder, the instance of ConverterControlloer is connected to the three fields properly, targeting (from top to bottom) rateField, dollarField, and totalField. the "convert" button is then, in turn, connected to the instance of ConverterController, targeting the action convert:. so much as I can tell, everything seems to be right so far.

back in Project Builder, editing converter.m, we have:

...
- (float)convertAmountfloat)amt atRatefloat)rate
{
return (amt * rate);
}
...


so... it seems to me, that from what the code in the tutorial says, the app should work as so: enter a float value into the first field, which is put into the variable rate; enter a float value into the second filed, which is put into the variable amt; press the "convert" button, and convert: returns a value (amt * rate) which is put into the variable total; total is then, in turn, put into the third field.

but... it's not working for me. I enter the values "2" and "2," expecting a return of "4," but no... I get "2" back, instead. hmmm....

I have pondered and poked at this thing for days upon days, and I'm at my wits' end. I hope someone can help me out here.

just in case, I'll include the .hs and .ms here so you can see what I'm working with:

ConverterController.h:

#import <Cocoa/Cocoa.h>

@interface ConverterController : NSObject
{
IBOutlet id converter;
IBOutlet id dollarField;
IBOutlet id rateField;
IBOutlet id totalField;
}
- (IBAction)convertid)sender;
@end


ConverterController.m:

#import "ConverterController.h"
#import "Converter.h"

@implementation ConverterController

- (void)awakeFromNib
{
[[rateField window] makeKeyAndOrderFront:self];
[rateField selectText:self];
}

- (IBAction)convertid)sender
{
float rate, amt, total;

amt = [dollarField floatValue];
rate = [rateField floatValue];

total = [converter convertAmount:amt atRate:rate];

[totalField setFloatValue:total];
[rateField selectText:self];
}

@end


Converter.h:

#import <Cocoa/Cocoa.h>

@interface Converter : NSObject
{
}
- (float)convertAmountfloat)amt atRatefloat)rate;

@end


Converter.m:

#import "Converter.h"
#import "ConverterController.h"
@implementation Converter
- (float)convertAmountfloat)amt atRatefloat)rate
{
return (amt * rate);
}
@end


Thanks, and I hope I'm not just being stupid...

------------------


[This message has been edited by rgoer (edited 04-21-2001).]
Even Einstein feared the power of "spooky action at a distance"
     
Mac Enthusiast
Join Date: Apr 1999
Location: Portland, Oregon
Status: Offline
Reply With Quote
Apr 21, 2001, 05:03 PM
 
Weird.. when I Did the currency converter tutorial it worked fine. Maybe you should start over from scratch? When I went through it, it took less than 2 hours total. 2 hours really isnt that long, when you've been trying to figure it out for "days upon days"
everything you know is wrong (and stupid)
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Computer Error: Unknown
Status: Offline
Reply With Quote
Apr 21, 2001, 05:36 PM
 
You can also do like I did, and just calculate the total in the ConverterController.m file:


ConverterController.m:

#import "ConverterController.h"
#import "Converter.h"

@implementation ConverterController

- (void)awakeFromNib
{
[[rateField window] makeKeyAndOrderFront:self];
[rateField selectText:self];
}

- (IBAction)convert id)sender
{
float rate, amt, total;

amt = [dollarField floatValue];
rate = [rateField floatValue];

total = amt * rate;

[totalField setFloatValue:total];
[rateField selectText:self];
}

@end


[This message has been edited by graphiteman (edited 04-21-2001).]
"...Because the people who are crazy enough to think they can change the world,
are the ones who do."
-To the Crazy Ones

     
Forum Regular
Join Date: Dec 2000
Status: Offline
Reply With Quote
Apr 21, 2001, 05:36 PM
 
Your code looks ok to me, so I'd suspect you have something connected wrong in your nib file.
Run the debugger. It's really neat. Just put a breakpoint at the start of convert, stick in your numbers and step through the code. That way you can make sure that amt and rate get set right, and you can see what total gets set to.
     
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status: Offline
Reply With Quote
Apr 21, 2001, 06:53 PM
 
I sounds like you never connected anything to your "converter" outlet. If you didn't, "total = [converter convertAmount:amt atRate:rate];" would either do nothing or crash the subroutine (without crashing the app itself).

------------------
     
rgoer  (op)
Mac Enthusiast
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 21, 2001, 09:53 PM
 
you were absolutely right, sonic blue! I forgot to actually press the connect button after control-dragging between ConverterController and Converter in the nib file. Thanks for pointing that out; now it works fine.

------------------
Even Einstein feared the power of "spooky action at a distance"
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 01:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2