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

mmzplanet

macrumors regular
Original poster
Nov 4, 2004
221
0
Florida
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
 

mduser63

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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;
}
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.

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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.

Shrug, I wouldn't use bindings for this, but I guess both methods work OK.
 

mmzplanet

macrumors regular
Original poster
Nov 4, 2004
221
0
Florida
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:
 

mmzplanet

macrumors regular
Original poster
Nov 4, 2004
221
0
Florida
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.