views:

82

answers:

3

Hello!

I'm having problems with a program I'm trying to make in Objective-C.

I have a method that I'm trying in my MainViewController.m that is as follows.

- (void)updateLabels:(NSString *)text :(BOOL)isOn; 
{
[self setNameLabel:(text *); 
if (isOn)
 [self setOnLabel:(ON *);
else
  [self setOnLabel:(OFF *); 
}

My goal should be obvious from this code. I'm problably doing something terribly wrong to get the error message Expected expression before ')' token.

I'd appreciate any help!

+3  A: 

This looks more like what you are trying to do, that is assign text to a label and then toggle whether its ON or OFF

#define ON YES
#define OFF NO
    - (void)updateLabels:(NSString *)text isOn:(BOOL)
        {
        [self setNameLabel:text]; 
        if (isOn)
         [self setOnLabel:ON];
        else
          [self setOnLabel:OFF]; 
        }
ennuikiller
Still got the semicolon at the end of the method declaration that is raising the error.
Matt Williamson
A: 

It should be:

 - (void)updateLabels:(NSString *)text status:(BOOL) isOn {
    ...

You need to remove the semi-colon after the method declaration. The second and subsequent parameters need both a field name (status) and a variable to put the value in (isOn).

In the interface declaration you swap the opening bracket for a semi-colon like this:

 - (void)updateLabels:(NSString *)text status:(BOOL) isOn;

Then calling it would look like this:

[... updateLabels:@"My label text" status:YES];
Derek Clarkson
-1 You don't need to remove the semicolon (believe it or not) and you can have a bare colon in a selector.
JeremyP
Oh, didn't know that. Whats the effect of leaving the semi-colon in and using bare colons in selectors? I'm presuming they would be classified as a less than desirable practice given I've not seen it done in any examples and books I've looked at :-)
Derek Clarkson
@Derek Clarkson: You're right, you *should* name all the parameters in the selector, and you'll rarely see code that doesn't.
mipadi
+2  A: 

A couple problems here. First, you don't need that semicolon. Second, you don't need to pass pointers for ON and OFF:

- (void)updateLabelsWithText:(NSString *)text AndOnState:(BOOL)isOn {
    [self setNameLabel: text]; 
    if(isOn)
        [self setOnLabel:@"ON"];
    else
        [self setOnLabel:@"OFF"];
}
Matt Williamson
The superfluous semicolon is actually legal believe it or not.
JeremyP
Good to know, I thought it threw errors
Matt Williamson