Help me with Objective-C

Discussion in 'Mac Programming' started by anonymous3025, May 3, 2008.

  1. anonymous3025 macrumors newbie

    Joined:
    May 2, 2008
    #1
    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
     
  2. CaptainZap macrumors regular

    Joined:
    Jan 17, 2007
    #2
    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
     
  3. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    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.
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    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.
     
  5. NexesDev macrumors newbie

    Joined:
    May 12, 2008
    #5
    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
     

Share This Page