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

Buschmaster

macrumors 65816
Original poster
Feb 12, 2006
1,306
27
Minnesota
So, I am new to Objective-C and I wrote a basic hello world program to get myself going and was able to do it without a problem. I have a textfield and a submit button and that changes the words to read "Hello, (name entered!)"

Here's my code for that:
Code:
-(void)clickbutton:(id)sender;
{
	NSString *text = [ textField stringValue ] ;

	if([ text length ] == 0) {
		text = @"world";
	}
	NSString *text2 ;
	text2 = [ NSString stringWithFormat: @"Hello, %@!" , text ] ;
	[ label setStringValue : text2 ] ;
}

What I want to do now is add a couple names where it will change the greeting. For example, if the name entered were Steve it would read "Hello, Steve, what kind of rapping name is Steve?"

So I would THINK (from other languages I know) the code would be:
Code:
-(void)clickbutton:(id)sender;
{
	NSString *text = [ textField stringValue ] ;

	if([ text length ] == 0) {
		text = @"world" ;
	} else if (text == @"Steve") {
		text = @"Steve, what kind of rapping name is Steve?" ;
	}
	NSString *text2 ;
	text2 = [ NSString stringWithFormat: @"Hello, %@!" , text ] ;
	[ label setStringValue : text2 ] ;
}
And this compiles no problem but then says "Hello, Steve!" rather than what I want it to...

I also tried:
Code:
-(void)clickbutton:(id)sender;
{
	NSString *text = [ textField stringValue ] ;

	if([ text length ] == 0) {
		text = @"world" ;
	} else if ([ textField stringValue ] == @"Steve") {
		text = @"Steve.  What kind of rapping name is Steve?" ;
	}
	NSString *text2 ;
	text2 = [ NSString stringWithFormat: @"Hello, %@!" , text ] ;
	[ label setStringValue : text2 ] ;
}

But no such luck...

I'm sure I'm just missing something stupid and basic, but I'm just trying to explore the language with basic (i.e. worthless) programming before getting serious and trying to write a program someone would actually want.

Thanks!:)
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
You will want to use one of the compare methods of NSString to do the comparison. Look at the docs for NSString: http://developer.apple.com/document...ng.html#//apple_ref/doc/uid/20000154-compare_

[Edit] More info: When you do something like (text == @"Steve), you are comparing pointer values (rather than comparing what is being pointed to, which is what you want). text points to a different location in memory than the literal @"Steve", so these are not equal, even it they both have the value @"Steve". The pointer comparison is useful when you want to know if two pointers point to the exact same instance of an object.
 

Buschmaster

macrumors 65816
Original poster
Feb 12, 2006
1,306
27
Minnesota
Got it to work with:
Code:
-(void)clickbutton:(id)sender;
{
	NSString *text = [ textField stringValue ] ;
	NSString *Steve = @"Steve";

	if([ text length ] == 0) {
		text = @"world" ;
	}
	if ([text isEqualToString: Steve]) {
		text = @"Steve.  What kind of rapping name is Steve?" ;
	}
	NSString *text2 ;
	
	text2 = [ NSString stringWithFormat: @"Hello, %@!" , text ] ;
	[ label setStringValue : text2 ] ;
}
Thank you! :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.