Re: If...then statements
Re: If...then statements
- Subject: Re: If...then statements
- From: Joe Osborn <email@hidden>
- Date: Sat, 4 Jun 2005 20:46:49 -0500
There remains some duplication you might want to remove, i.e. the
'[loc setStringValue:...];' and '[act setStringValue...]'. Here's a
way you might do that:
NSDictionary *source = [NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:@"Ground Floor",
@"loc",
@"You decide to ... Number 2", @"act", nil], @"Lab",
[NSDictionary dictionaryWithObjectsAndKeys:@"Lab", @"loc",
@"You walk into ... don't enter", @"act", nil],
@"Welcome Screen", nil];
NSDictionary *room = [source objectForKey:[loc stringValue]];
NSString *locString = [room objectForKey:@"loc"];
NSString *actString = [room objectForKey:@"act"];
[loc setStringValue:locString];
[act setStringValue:actString];
Better yet, put this into a file in the 'ascii' or XML .plist format,
and make the dictionary with [NSDictionary
dictionaryWithContentsOfFile:]. Or make proper Room objects and
construct the dictionary elsewhere.
Hope this is useful in making a more extensible solution than
perpetually appending else if() statements.
__joe
On Jun 4, 2005, at 17.01, Connor Wakamo wrote:
Thank you, problem solved!
Connor
On Jun 4, 2005, at 5:52 PM, Steve Palmer wrote:
You should use isEqualToString to compare the strings. For example:
if([[loc stringValue] isEqualToString:@"Lab"])
- Steve
On Jun 4, 2005, at 2:44pm, Connor Wakamo wrote:
Hello,
I am using an If statement to compare the stringValue of a
NSTextField to another string to determine what to do next. It
does what's inside of the If statement regardless of whether it's
true or not. So, if the stringValue of loc is equal to Lab (or
anything, really) it executes both pieces of the code. Here's
the code I'm using:
if([loc stringValue] == @"Lab")
{
[loc setStringValue:@"Ground Floor"];
[act setStringValue:@"You decide to enter the simulator
and do his Adventure.\nSCIENTIST: Very well, step inside and I'll
get you in there. The old way of succeeding won't work anymore,
so you've got to find the new way out. Instead of the Finish
portal, there is a Quit portal. The Finish portal is in a
completely different place.\nYou then are put into the Tutorial
Tower and see the same sign from before. You skip reading it and
press the right button. Two doors open, however.\nYou now have a
choice. You can either go through door number 1 or door number 2.
\nOption 1 - Door Number 1\nOption 2 - Door Number 2"];
}
if([loc stringValue] == @"Welcome Screen")
{
[loc setStringValue:@"Lab"];
[act setStringValue:@"You walk into a lab.\nSCIENTIST:
Hello, hello, hello! Welcome to my lab! I've set up a new
Tutorial Tower... Do you want to go in?\nOption 1 - Enter\nOption
2 - Don't Enter"];
}
Thanks,
Connor
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40opencommunity.co.uk
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden