Need a little help with a Cocoa app I'mm writing

Posted:
in Genius Bar edited January 2014
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>

Comments

  • Reply 1 of 4
    airslufairsluf Posts: 1,861member
  • Reply 2 of 4
    ok, i see a few things in the implementation part [the interface seems fine].



    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' . i think its necessary to have 'self' there because objc needs to know which object to send the "convertAmount" message to. in this case, i think you want to send it to the 'self' object (similar to 'this' in c++). i'm not sure if objc knows to imply self if an object is lacking.



    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>
  • Reply 3 of 4
    [quote]In your post it looks like you have an extra space in "float total = [convertAmount istance fuelCap:capacity];" and the case is different as well.



    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 ' '. It automatically changes the case in UBB Code to uppercase. The actual code was written much better



    [quote]note the addition of 'self', and the proper spelling of 'distance' . i think its necessary to have 'self' there because objc needs to know which object to send the "convertAmount" message to. in this case, i think you want to send it to the 'self' object (similar to 'this' in c++). i'm not sure if objc knows to imply self if an object is lacking.<hr></blockquote>



    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?
  • Reply 4 of 4
    ibrowseibrowse Posts: 1,749member
    [quote]Originally posted by DoctorGonzo:

    <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.
Sign In or Register to comment.