1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

XCode Help

Discussion in 'Mac Programming' started by mmzplanet, Aug 7, 2007.

  1. macrumors regular

    mmzplanet

    #1
    I am making a program to that basically does a calculation (making a little points calculator for some family).... obviously I am new to Obj-C and am using other code as a guide to learning my own. The Interface is fine..... but my code seems screwed. I am taking 3 fields and calculating and then presenting the output. Anytime the a field changes it needs to recalculate. here is the code....

    Any advice on my errors?

    UPDATE: Made a couple of changes... Still does not react like I was expecting.
    Code:
    #import "PointCalculator.h"
    
    @implementation PointCalculator
    
    + (void)initialize
    {
    	[PointCalculator setKeys:
    		[NSArray arrayWithObjects:@"inputFiber", nil]
    		triggerChangeNotificationsForDependentKey:@"outputPoints"];
    	[PointCalculator setKeys:
    		[NSArray arrayWithObjects:@"inputCalories", nil]
    		triggerChangeNotificationsForDependentKey:@"outputPoints"];
    	[PointCalculator setKeys:
    		[NSArray arrayWithObjects:@"inputFat", nil]
    		triggerChangeNotificationsForDependentKey:@"outputPoints"];
    }
    - outputPoints
    {
    		result = inputCalories+inputFat+inputFiber;
    		return result;
    }
    
    @end
    
    Code:
    /* PointCalculator */
    
    #import <Cocoa/Cocoa.h>
    
    @interface PointCalculator : NSObject
    {
    		int inputCalories;
    		int inputFat;
    		int inputFiber;
    		int result;
    }
    
    	- outputPoints;
    
    @end
    
     
  2. macrumors 68040

    mduser63

    #2
    Rule #1 when asking for help: tell us what the problem is.
    Rule #2: "It doesn't work" is not a valid statement of the problem.

    Seriously, without any idea of what is wrong, nobody is going to be inclined to help. It's also good to post all your code, not just a small snippet. Zipping up the entire project and posting a link to it might be a good idea, especially if you're using things like Cocoa Bindings.

    All that said, you have four instance variables in your PointCalculator class. Where are your accessor methods (setInputCalories, inputCalories, etc)? How are you connecting the fields in your interface to the variables in your class? Are you using bindings?

    We need a lot more to work with.
     
  3. macrumors regular

    mmzplanet

    #3
  4. Moderator emeritus

    kainjow

    #4
    Change your initialize method to this:

    Code:
    + (void)initialize
    {
    	[PointCalculator setKeys:
    		[NSArray arrayWithObjects:@"inputFiber", @"inputCalories", @"inputFat", nil]
    		triggerChangeNotificationsForDependentKey:@"outputPoints"];
    }
    And write your methods properly to avoid warnings and improve clarity. There is no need to declare result as a class variable:

    Code:
    - (int)outputPoints
    {
    	int result = inputCalories + inputFat + inputFiber;
    	return result;
    }
     
  5. macrumors 603

    whooleytoo

    #5
    I think what mduser63 meant was just to tell us what behaviour/results you were expecting, and what you saw. Obviously you already know something is wrong, or you wouldn't be posting here. ;)
     
  6. macrumors G4

    Eraserhead

    #6
    I think you have missed out learning about some of the basics of Cocoa, PointController is the controller and you need to create an IBAction method which is connected from the text boxes in the nib. You can then get the values from the text boxes using [textBoxName intValue], the best way to do this is to stick some IBOutlet's in the header file to point to the text boxes (NSTextFields), and another IBOutlet to point to the results box. You then use [resultBox setIntValue:result]; to set the result.

    Reading through something like this would probably be good.
     
  7. Moderator emeritus

    kainjow

    #7
    No you don't need IBOutlets in this situation. It is all done through bindings. If you download the project and make the change to initialize I mentioned, it works.
     
  8. macrumors G4

    Eraserhead

    #8
    Shrug, I wouldn't use bindings for this, but I guess both methods work OK.
     
  9. macrumors regular

    mmzplanet

    #9
    Thanks for all the advice everyone. I'm glad there is a place like this where everyone is this helpful.

    As soon as I get home I will give it a shot. Yeah I am new to Cocoa but sometimes I like to learn by getting thrown into it. I can write basic text programs right and left all day. I am new to this object oriented programming. I have enjoyed PHP and MySQL so much, it made me interested in trying to make some apps. :D

    :apple:
     
  10. macrumors regular

    mmzplanet

    #10
    One more thing, how do I get it to acknowledge an empty field as zero. When the field gets blanked it generates the following in the run log.

    Code:
    2007-08-08 23:51:35.072 Points[20492] [<PointCalculator 0x35c640> setNilValueForKey]: could not set nil as the value for the key inputCalories.
     
  11. macrumors G4

    Eraserhead

    #11
    I'm not sure, but my method would handle this fine :p ;).

    You could also try binding the term to an NSNumber rather than an int, as that can handle being nil, alternatively set the null placeholder in the bindings to 0.
     

Share This Page