views:

713

answers:

2

Hi all,

I'm trying to make an app that will respond to your command when inserted. So you type in any text in the first box and press enter. It will respond with a response in the 2nd field. I'm not sure how the coding is done here. I'm having trouble with the "if inputbox text = @"whatever", I'm pretty sure that is completely off. Here is the code I have so far (not iphone sdk):

#import "HeliosControl.h"

@implementation HeliosControl
- (IBAction)quitButton:(NSButton *)sender {

}

- (IBAction)sendButton:(NSButton *)sender {

    if (inputBox *********)  // <------ What do I put in for the asterisks?
    {
     [outputBox setStringValue:@"Welcome to the SYSTEM"];
    }
    else
    {
     [outputBox setStringValue:@"I do not understand your command."];
    }

}
@end

BTW I'm a complete noob since I started Objective-C like a week ago.

Second Question:

This is a very simple one, but what would be the coding for closing an application? This is for my quit button.

+9  A: 

You want if ([[inputBox stringValue] isEqualToString:@"whatever"]) (assuming inputBox is an NSTextField — otherwise, use the appropriate method for that class to get a string out of it).

Oh, and you can quit the application with [NSApp terminate:self].

Chuck
Thanks chuck for the code! It's finally working. It's just the [NSApp terminate:self] that gives me an error code.
Kevin
@Chuck, looks like Java but with strange syntax. Is that supposed to be `@"whatever]` or rather `@"whatever"`? For the Objective-C Curious...
Yar
@Yar: Yes, it is a bit like Java, because Java was based on Objective-C. And I was missing the close quote.
Chuck
@Chuck thanks, now I feel a bit more inclined to learn it.
Yar
Careful with quitting the application from your code. You are not supposed to do that, only the user can decide to do it (by pressing the home button). The code in this answer will work, but is a private API and Apple might decide to reject your app because of it.
Guillaume
+3  A: 

Chuck's answer is spot on, but I thought it worth expanding on why you've had problems. There are a number of mistakes in your line:

"if inputbox text = @"whatever"

a) In Objective C you have to use == to check if x is equal to y. So the if statement would be:

if (myFirstVariable == mySecondVariable) { // Do something }

b) A string variable is actually a more complicated thing than a variable just holding a number. That variable's value will actually be the memory address where it is stored. Also, you will usually actually only be using a pointer (denoted by the * when you declare a variable) to the variable.

This means that if you type the following:

if (myFirstVariable == @"Some text")

or

if (myFirstStringVariable == mySecondStringVariable)

Then you're actually only checking for whether they both point to the same bit of memory! Not whether the text is the same. This is why as Chuck explained you need to use the [isEqualToString] method.

Hope that helps!

h4xxr
The bit about the string's value isn't quite correct. The *variable's* value is the address of the string, which is the number you referred to. The string's value, as far as you can say a composite type has one value, is indeed the string of characters.
Chuck
Fair, edited for clarity
h4xxr
Nice. I'd already upvoted anyway. I just like to make sure people understand about pointers, because a lot of Objective-C beginners have a heck of a time wrapping their heads around what they are and what they mean.
Chuck