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 May 30, 2009, 09:35 AM   #1
bguy
macrumors newbie
 
Join Date: May 2009
Cocoa Number Guesser Help

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

Last edited by bguy; May 31, 2009 at 08:58 AM. Reason: changed code
bguy is offline   0 Reply With Quote
Old May 30, 2009, 09:51 AM   #2
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 09:59 AM   #3
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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.
bguy is offline   0 Reply With Quote
Old May 30, 2009, 10:06 AM   #4
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 10:09 AM   #5
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
Quote:
Originally Posted by SRossi View Post
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.
How do you do that?
bguy is offline   0 Reply With Quote
Old May 30, 2009, 10:19 AM   #6
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 10:36 AM   #7
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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.
bguy is offline   0 Reply With Quote
Old May 30, 2009, 10:44 AM   #8
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 10:55 AM   #9
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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
bguy is offline   0 Reply With Quote
Old May 30, 2009, 10:59 AM   #10
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 11:03 AM   #11
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
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
lee1210 is offline   0 Reply With Quote
Old May 30, 2009, 12:06 PM   #12
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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

Last edited by bguy; May 31, 2009 at 08:59 AM. Reason: changed cde
bguy is offline   0 Reply With Quote
Old May 30, 2009, 12:19 PM   #13
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
I take it that Secret and Guess are functions that return the random number and the number guessed?

Overall the code looks fine.

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 12:21 PM   #14
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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.
bguy is offline   0 Reply With Quote
Old May 30, 2009, 12:26 PM   #15
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
I would also do @property(readwrite, assign);

Try that.

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 12:48 PM   #16
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
It is still not comparing correctly.
bguy is offline   0 Reply With Quote
Old May 30, 2009, 12:56 PM   #17
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 30, 2009, 01:07 PM   #18
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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.
bguy is offline   0 Reply With Quote
Old May 30, 2009, 03:40 PM   #19
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
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
lee1210 is offline   0 Reply With Quote
Old May 31, 2009, 07:54 AM   #20
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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?
bguy is offline   0 Reply With Quote
Old May 31, 2009, 08:14 AM   #21
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Why not do:

Code:
int compareNumber = [self Compare];
Then use the if statement on the compareNumber.

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old May 31, 2009, 08:15 AM   #22
JoshDC
macrumors regular
 
Join Date: Apr 2009
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: )
JoshDC is offline   0 Reply With Quote
Old May 31, 2009, 08:17 AM   #23
GorillaPaws
macrumors 6502a
 
GorillaPaws's Avatar
 
Join Date: Oct 2003
Location: Richmond, VA
Tripple-check your connections in IB.
GorillaPaws is offline   0 Reply With Quote
Old May 31, 2009, 08:53 AM   #24
bguy
Thread Starter
macrumors newbie
 
Join Date: May 2009
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?

Last edited by bguy; May 31, 2009 at 09:00 AM.
bguy is offline   0 Reply With Quote
Old May 31, 2009, 09:02 AM   #25
JoshDC
macrumors regular
 
Join Date: Apr 2009
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.
JoshDC is offline   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
Xcode build phase - unknown number of input files to unknown number of output files TheChekt Mac Programming 0 Dec 15, 2013 08:42 AM
Your Apple store order number is also your UPS reference number Gjwilly iPhone 0 Sep 23, 2013 09:36 AM
4S- Changed phone number, old number still shows up in messages dbabytro iPhone 4 Jul 18, 2013 01:58 PM
Serial number or phone number? davidra iPhone Tips, Help and Troubleshooting 1 Mar 13, 2013 10:04 PM
Apple Mail: number of messages in inbox doesn't match number of messages on server joshwillmarth OS X 10.8 Mountain Lion 0 Oct 24, 2012 10:18 PM

Forum Jump

All times are GMT -5. The time now is 11:05 AM.

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

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