Need a little help with a Cocoa app I'mm writing
First off, I'm very new at this, second of all, I know my code is probably an unelegant POS, so please, bear with me here. (I did write it at 1am, BTW)
The goal of this app is to divide the distance of a trip by fuel capacity of a particular car. A very basic app, just to practice my coding skills.
The daysField is where the results of the calcuations on fuelField and milesField are dumped. (it is called daysField because I origionally had other ideas about what I was going to do with the app)
The idea is simply to assign the values the user enters to a pointer, which proves to be no problem. When it comes to dividing them, I can't seem to get it to perform the calculation. It can't even recognize convertAmount.
MyController.h:
[quote]/* MyController */
#import <Cocoa/Cocoa.h>
@interface MyController : NSObject
{
IBOutlet NSTextField *daysField;
IBOutlet NSTextField *fuelField;
IBOutlet NSTextField *milesField;
}
- (float)convertAmount
float)distance fuelCap
float)capacity;
- (IBAction)convert
id)sender;
@end<hr></blockquote>
MyController.m
[quote]#import "MyController.h"
@implementation MyController
- (IBAction)convert
id)sender
{
float capacity = [fuelField floatValue];
float distance = [milesField floatValue];
float total = [convertAmount
istance fuelCap:capacity];
[milesField setFloatValue:total];
}
- (float)convertAmount
float)distance fuelCap
float)capacity
{
return (distance / capacity);
}
@end<hr></blockquote>
I've tried making the latter instance method a simple (float)blahBlahBlah, but nothing seems to work.
[ 12-30-2002: Message edited by: DoctorGonzo ]</p>
The goal of this app is to divide the distance of a trip by fuel capacity of a particular car. A very basic app, just to practice my coding skills.
The daysField is where the results of the calcuations on fuelField and milesField are dumped. (it is called daysField because I origionally had other ideas about what I was going to do with the app)
The idea is simply to assign the values the user enters to a pointer, which proves to be no problem. When it comes to dividing them, I can't seem to get it to perform the calculation. It can't even recognize convertAmount.
MyController.h:
[quote]/* MyController */
#import <Cocoa/Cocoa.h>
@interface MyController : NSObject
{
IBOutlet NSTextField *daysField;
IBOutlet NSTextField *fuelField;
IBOutlet NSTextField *milesField;
}
- (float)convertAmount


- (IBAction)convert

@end<hr></blockquote>
MyController.m
[quote]#import "MyController.h"
@implementation MyController
- (IBAction)convert

{
float capacity = [fuelField floatValue];
float distance = [milesField floatValue];
float total = [convertAmount

[milesField setFloatValue:total];
}
- (float)convertAmount


{
return (distance / capacity);
}
@end<hr></blockquote>
I've tried making the latter instance method a simple (float)blahBlahBlah, but nothing seems to work.
[ 12-30-2002: Message edited by: DoctorGonzo ]</p>
Comments
on the line that declares the 'total' variable, i think it should be like this:
[code]float total = [self convertAmount: distance fuelCap:capacity];</pre><hr></blockquote>
note the addition of 'self', and the proper spelling of 'distance'
also, in your method "convertAmount", you do a division by a variable "capacity". teacher always told me to ensure the denominator is not zero before dividing by it, though not really necessary if you can guarantee it otherwise.
furthermore, since "convertAmount" doesn't act on any private members of any MyController objects, it would probably be good to make it a static member. instead of "-convertAmount...", use "+convertAmount..." [in both the interface and implementation, but not in calling the function]. then, in the above correction i made, you wouldn't use 'self', you would use 'MyController'. static members don't require that you instantiate an instance of an class. the message actually goes to the class [which itsef is maintained in memory similar to an instance], instead of a specific instance of a class.
[ 12-30-2002: Message edited by: thuh Freak ]</p>
Sometimes the stupidest little things bite you. Didn't see anything else that was obvious in a ten second scan, that stood out though.<hr></blockquote>
Blame UBB! When I first posted it, I forgot to turn off smilies, and it thought I was trying to type a '
[quote]note the addition of 'self', and the proper spelling of 'distance'
That worked! Thanks!
It turns out I also needed to create a text field and method for dealing with miles per gallon (the things you forget to do at 1am). That took all of 4 seconds.
I seem to be much better at writing Foundation Tools, for some reason. But I'm getting better at making snazzy apps with Interface Builder.
This might sound like a dumb question, but should an app like the one discussed here really take %16 of CPU time?
<strong>This might sound like a dumb question, but should an app like the one discussed here really take %16 of CPU time?</strong><hr></blockquote>
I really don't think so, check your memory releases.