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

budapester

macrumors newbie
Original poster
Aug 25, 2010
6
0
Budapest
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!
 
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).
 
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!"];
 
Thank you, deleting (textToDisplay) solved it -- and I even understand why! ;)

Code:
@implementation Message

- (void) displayMessage: txt
{
    NSLog (@"%@", txt);
}

@end //Message


Thank you again
 
Thank you, deleting (textToDisplay) solved it -- and I even understand why! ;)

Code:
@implementation Message

- (void) displayMessage: txt
{
    NSLog (@"%@", txt);
}

@end //Message

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);
}
 
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);
}


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.