Obj-C Compiler Error: Expected ')' before 'textToDisplay'

Discussion in 'Mac Programming' started by budapester, Aug 25, 2010.

  1. budapester macrumors newbie

    Joined:
    Aug 25, 2010
    Location:
    Budapest
    #1
    I am new to programming, and trying to tweak a very simple Hello World program which runs in the Console.

    I get the same error twice in the following code:


    Code:
    #import <Foundation/Foundation.h>
    
    @interface Message : NSObject
    
    - (void) displayMessage: (textToDisplay) txt;   // error: Expected ')' before 'textToDisplay'
    
    @end //Message
    
    
    @implementation Message
    
    - (void) displayMessage: (textToDisplay) txt    // error: Expected ')' before 'textToDisplay'
    {
        NSLog (@"%@", txt);
    }
    
    @end //Message
    
    
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        Message * Messenger = [Message new];
        [Messenger displayMessage: (@"Hello, World!")];
    	
        [pool drain];
        return 0;
    }
    I can't figure out why; the syntax seems to be correct and logical for me -- but obviously it isn't :eek:

    Guidance would be greatly appreciated!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The string inside the brackets indicates the type of the argument. There is no type "textToDisplay" unless you have defined one yourself (you haven't).
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    This line is wrong:

    Code:
    [Messenger displayMessage: (@"Hello, World!")];
    Get rid of the parenthesis. They are not needed here. It should look like this:

    Code:
    [Messenger displayMessage: @"Hello, World!"];
     
  4. budapester thread starter macrumors newbie

    Joined:
    Aug 25, 2010
    Location:
    Budapest
    #4
    Thank you, deleting (textToDisplay) solved it -- and I even understand why! ;)

    Code:
    @implementation Message
    
    - (void) displayMessage: txt
    {
        NSLog (@"%@", txt);
    }
    
    @end //Message

    Thank you again
     
  5. bobthedino macrumors member

    Joined:
    Jan 11, 2006
    #5
    Although now the method argument called "txt" has no explicit type, so it is assumed to be of type "id" (or "pointer to an object"). So your displayMessage method will actually accept any type of object as its "txt" parameter, and as it happens the %@ in your NSLog statement will happily handle it.

    You might want to make your method expect an object of type NSString, like this:

    Code:
    - (void)displayMessage:(NSString *)txt {
        NSLog(@"%@", txt);
    }
    
     
  6. budapester thread starter macrumors newbie

    Joined:
    Aug 25, 2010
    Location:
    Budapest
    #6

    Yes, that makes perfect sense.
    As you can imagine from the type of application, I was happy that I/we made it work... But this helps taking it one step further.
     

Share This Page