PDA

View Full Version : Help with basic if statement




Buschmaster
Mar 11, 2008, 03:11 PM
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:
-(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:
-(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:-(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
Mar 11, 2008, 03:20 PM
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/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.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
Mar 11, 2008, 03:49 PM
Got it to work with:
-(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! :)