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

anonymous3025

macrumors newbie
Original poster
May 2, 2008
6
0
Hello, I am new to objective-C and Cocoa. I tried to make an app where you enter text and there is an if statement that decides what happens. Except even if a enter the correct info the else statement still executes. Here is the code:

//This is the header file
#import <Cocoa/Cocoa.h>

@interface Who : NSObject {
IBOutlet id input;
IBOutlet id text;
}
- (IBAction)who:(id)sender;
@end


//This is the implementation file
#import "Who.h"

@implementation Who
- (IBAction)who:(id)sender {
if (input==@"Tim")
{
[text setStringValue:mad:"Hi tim"];
}
else
{
[text setStringValue:mad:"I do not know you"];
}
}
@end
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
If you want to compare two strings, the == doesn't work because input is just a pointer to the data. So you need to use the [string isEqualTo: string2] which should work for the if statement.

Edit: Oh and if your input is a NSTextField you should use the stringValue method... So your if statment should look like this.
Code:
if ([[input stringValue] isEqualToString: @"Tim"]) {
	[text setStringValue:@"Hi tim"];
}
else {
	[text setStringValue:@"I do not know you"];
}

And for future reference, try to include your code in tags, it makes it easier to read :p
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Code:
//This is the header file
#import <Cocoa/Cocoa.h>

@interface Who : NSObject {
	IBOutlet id input;
	IBOutlet id text;
}
- (IBAction)who:(id)sender;
@end


//This is the implementation file
#import "Who.h"

@implementation Who
- (IBAction)who:(id)sender {
	if (input==@"Tim")
	{
		[text setStringValue:@"Hi tim"];
	}
	else
	{
		[text setStringValue:@"I do not know you"];
	}
}
@end

Things just don't work that way in objective C, my friend.

The 'id' is actually a pointer to an object. Pointers have different behaviors or operators such as '=='. I think you should look into pointers. Understand them completely before proceeding to learning ObjC, because problems may arise later with memory management.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Don't use id as the type for the point, use NSTextField or whatever...

And you use [foo isEqualTo:mad:"bar] to check equality of objective C objects.
 

NexesDev

macrumors newbie
May 12, 2008
14
0
Your code should look like this

Your code should look something like this, it will work.

Header file:

#import <Cocoa/Cocoa.h>


@interface mainController : NSObject
{
IBOutlet NSTextField *inputText;
IBOutlet NSTextField *displayText;
}

-(IBAction)sendText:(NSButton *)sender;

@end

Main File :

@implementation mainController

-(IBAction)sendText:(NSButton *)sender
{
if ([[inputText stringValue] isEqualTo:mad:"Tim"])
{
[displayText setStringValue:mad:"I know you"];
}
if ([[inputText stringValue] isNotEqualTo:mad:"Tim"])
{
[displayText setStringValue:mad:"I dont know you"];
}

}

@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.