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

bguy

macrumors newbie
Original poster
May 30, 2009
28
0
Hello, I am a new Cocoa programmer and I am having problems with a number guesser program. I have set up and linked all the actions to the interface, but when I enter a guess and press the button I set up, nothing happens. I assume that means something in the action's programming is messed up. I am also new to Objective-C so I wouldn't know if I messed up. I am supplying the GuesserController.m file, but if you need to check anything else, I can supply it. Could someone explain to me what is wrong?

Code:
#import "GusserController.h"


@implementation GusserController
- (IBAction)Guess:(id)sender
{
	guesser = [[Guesser alloc] init];
	[guesser setGuess:[GuessField intValue]];
	if ([guesser Compare] == 0)
	{
		[ResponceField setStringValue:@"Congrats, you got it!"];
		if ([guesser Guesses] < [guesser Highscore])
			[HighscoreField setIntValue:[guesser Guesses]];
		if ([guesser Guesses] > [guesser Lowscore])
			[LowscoreField setIntValue:[guesser Guesses]];
		[guesser setGuesses:1];
		[self SetRand:(id)sender];
	}
	else if ([guesser Compare] == -1)
	{
		[ResponceField setStringValue:@"Guess Higher"];
		[guesser setGuesses:([guesser Guesses] + 1)];
	}
	else if ([guesser Compare] == 1)
	{
		[ResponceField setStringValue:@"Guess Lower"];
		[guesser setGuesses:([guesser Guesses] + 1)];
	}
	[GuessField selectText:self];
}
-(IBAction)SetRand:(id)sender
{
	srand( time(NULL));
	guesser = [[Guesser alloc] init];
	int randomNumber;
	randomNumber = rand() % 10 + 1;
	[guesser setSecret:(randomNumber)];
	NSLog(@"random number %d", randomNumber);
}
@end
 
I'm no expert at objective-c but with the compare method shouldn't it actually be ...

Code:
[guesser Compare:aString]

I am sure someone will disagree but im sure you need something to compare it with.

Stephen
 
No, compare is a function I have in a different file that compares two inputs and tells if your guess is higher, lower or equal.
 
Ah right sorry its just because u can use the compare method to find if they are equal. What I would do is set up NSLogs to make sure that numbers are being set up and that the application is taking in your guesses.

Stephen
 
Code:
NSLog(@"random number %@", urRandom);

That will post a message onto the console of the random number. Just change urRandom to the random number you have set up.

Stephen
 
I did as you suggested and it said that the random number is null. I can't figure out what is wrong.
I also changed the code a bit so it shouldn't reset the number every time you guess. I edited my first post to show my changes.
 
Right to create a random number you do:

Code:
int randomNumber;
randomNumber = random() %10 + 1;


This will create a random number, implement this into your SetRand function and then do

Code:
NSLog(@"Random number %d", randomNumber);

Im just saying this on the basis of the code that you have given, and earlier when i said %@ in the NSLog i meant %d sorry. If you do the code above that will give you a random number.

Stephen
 
Changed it again.
The random number generator works, but the guessing field doesn't. When I input a number and press it, in the console it says:
2009-05-30 11:50:56.407 InterfaceNumberGuesser[492:10b] *** -[NSTextField setString:]: unrecognized selector sent to instance 0x1282f0
2009-05-30 11:50:56.408 InterfaceNumberGuesser[492:10b] *** -[NSTextField setString:]: unrecognized selector sent to instance 0x1282f0
 
To set the string of a text field you need to do

Code:
[textField setStringValue:@"Hello"];

Notice setStringValue and the @ symbol before the text brackets

Stephen
 
SRossi beat me to it, but setString didn't work because NSTextField does not respond to this. You should have gotten a compiler warning about this. You should take a look at the docs for NSTextField and NSControl (its parent) so you know what you have to work with.

-Lee
 
Thanks for all the help. I am still having problems, but I think I know what they are, just not how to fix them. When I run the application, no matter what I guess, it always shows up as the correct answer. I know this is not the case since the NSLog tells me what the number is. I assume this means there is a problem with the Compare method I wrote. Here it is:
Code:
#import "Guesser.h"


@implementation Guesser
@synthesize Secret, Guess, Highscore, Lowscore, Guesses;

- (int)Compare
{
	NSLog(@"Is secret less? %d\tIs guess less? %d",[self Secret] < [self Guess],[self Guess] < [self Secret]);
	if ([self Secret] > [self Guess])
		return -1;
	else if ([self Secret] < [self Guess])
		return 1;
	else
		return 0;
}

@end
 
I used @property(readwrite) and synthesize like a tutorial told me to do for a different program. That might be the problem if I am getting the functions wrong.
 
Do you need to use @property or could you just use:

Code:
- (NSString)foo
- (void)setFoo:(NSString)aString

Because thats all that @propert and @synthesize is doing is taking away those methods
 
I don't think it is the problem, I believe I just wrote my code wrong, so I was wondering if people can check it. Besides the header files and the nib file it is all there.
 
What is the type of Guess and Secret (and why is the leading letter capitalized?). Print their values in Compare (again... capitalized... camelCase of the lower variety is "the way"), and see what they are. Print the values of your conditionals and see what they are:
Code:
NSLog(@"Is secret less? %d\tIs guess less? %d",[self secret] < [self guess],[self guess] < [self secret]);

-Lee
 
They are both integers. I added the code you suggested and i doesn't show up. I think that means Compare isn't being called. Why is that?
 
Have you initialized the guesser anywhere? Looks like you haven't...

You'll need to put: guesser = [[Guesser alloc] init] in the init: or awakeFromNib: method of the GusserController.

Just as a point of semantics, methods generally start lower case (e.g. Compare: should really be compare: )
 
I added the initialization, but now the compare is saying the secret number is always higher than the guess, no matter what the number is. Is there a fundamental flaw in the program?
 
Simply declaring it creates a pointer that points nowhere (0x0 or nil). The alloc method allocates memory for the new object, the init method sets everything to their default values. Both method returns an id (or pointer) that points to the created instance of the object. In Objective-C anything sent to a nil object will return nil (or 0). That's why your compare method was always returning 0.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.