Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 7, 2007, 08:51 PM   #1
mmzplanet
macrumors regular
 
Join Date: Nov 2004
Location: Florida
Send a message via AIM to mmzplanet
XCode Help

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

Last edited by mmzplanet; Aug 7, 2007 at 10:34 PM. Reason: Changes to code
mmzplanet is offline   0 Reply With Quote
Old Aug 7, 2007, 10:54 PM   #2
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
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.
mduser63 is offline   0 Reply With Quote
Old Aug 8, 2007, 06:03 AM   #3
mmzplanet
Thread Starter
macrumors regular
 
Join Date: Nov 2004
Location: Florida
Send a message via AIM to mmzplanet
Here is the entire project.

http://www.mmzplanet.org/Points.zip

I would love to post what is exactly wrong. I will try to post more info.
mmzplanet is offline   0 Reply With Quote
Old Aug 8, 2007, 08:08 AM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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;
}
kainjow is offline   0 Reply With Quote
Old Aug 8, 2007, 08:19 AM   #5
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
Quote:
Originally Posted by mmzplanet View Post
I would love to post what is exactly wrong. I will try to post more info.
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.
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Aug 8, 2007, 09:12 AM   #6
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: 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.

Last edited by Eraserhead; Aug 8, 2007 at 09:36 AM. Reason: Clarity
Eraserhead is online now   0 Reply With Quote
Old Aug 8, 2007, 09:42 AM   #7
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by Eraserhead View Post
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.
kainjow is offline   0 Reply With Quote
Old Aug 8, 2007, 11:27 AM   #8
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by kainjow View Post
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.
Eraserhead is online now   0 Reply With Quote
Old Aug 8, 2007, 12:34 PM   #9
mmzplanet
Thread Starter
macrumors regular
 
Join Date: Nov 2004
Location: Florida
Send a message via AIM to mmzplanet
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.

mmzplanet is offline   0 Reply With Quote
Old Aug 8, 2007, 10:54 PM   #10
mmzplanet
Thread Starter
macrumors regular
 
Join Date: Nov 2004
Location: Florida
Send a message via AIM to mmzplanet
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.
mmzplanet is offline   0 Reply With Quote
Old Aug 9, 2007, 05:08 AM   #11
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
I'm not sure, but my method would handle this fine .

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.
Eraserhead is online now   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
The App Store app thinks Xcode is installed, but I deleted Xcode. moonman239 Mac Applications and Mac App Store 3 May 6, 2014 05:53 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Xcode 4.5 iNeedAnsurs iPhone 0 Jan 25, 2013 02:49 PM
Xcode 3 vs Xcode 4 when dealing with Objective-C timebourne Mac Programming 8 Sep 23, 2012 05:36 PM
XCode 4.4.1 to Xcode 4.5 in Mountain Lion VinegarTasters OS X 10.8 Mountain Lion 2 Sep 19, 2012 08:55 PM

Forum Jump

All times are GMT -5. The time now is 06:32 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC