Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 11, 2008, 04:11 PM   #1
Buschmaster
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
Help with basic if statement

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!
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Mar 11, 2008, 04:20 PM   #2
iSee
macrumors 68040
 
iSee's Avatar
 
Join Date: Oct 2004
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/documenta...00154-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.

Last edited by iSee; Mar 11, 2008 at 04:26 PM.
iSee is offline   0 Reply With Quote
Old Mar 11, 2008, 04:49 PM   #3
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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!
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster 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
If-else statement not evaluating alexatwater iPhone/iPad Programming 2 Mar 13, 2014 02:38 PM
applescript if statement wild4life2013 Mac Programming 2 Aug 6, 2013 12:16 AM
Free note app with basic formatting basic HTML Subu iPhone and iPod touch Apps 0 Mar 1, 2013 04:44 AM
Can someone explain this statement DMB15 iPhone 6 Sep 12, 2012 06:41 PM

Forum Jump

All times are GMT -5. The time now is 12:29 PM.

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

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