Question

I have an iPad app in which I'm setting the shadow color of a UILabel in a UIView's initWithFrame: method. When I use the following syntax:

m_label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];

I get this compiler error:

Expected identifier before '[' token

However, when I use the following syntax:

[m_label setShadowColor:[UIColor colorWithWhite:1.0 alpha:0.5]];

It compiles without complaint.

Using property syntax for other properties of the UILabel is working fine (shadowOffset, autoresizingMask, backgroundColor, font, textColor, etc.).

Incidentally, I get the same error message when the statement is simply this:

m_label.shadowColor;

Whereas this, for example, gives no error:

m_label.shadowOffset;

FWIW, the entire method looks like this:

#define shadowColor        [UIColor colorWithWhite:1.00 alpha:0.5]
#define selectedColor      [UIColor colorWithWhite:0.25 alpha:1.0]
#define unselectedColor    [UIColor colorWithWhite:0.45 alpha:1.0]
#define CLOSEBUTTON_WIDTH  26.0
#define CLOSEBUTTON_HEIGHT 26.0

- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        m_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)];
        m_imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        m_imageView.backgroundColor = [UIColor clearColor];
        m_imageView.image = [[UIImage imageNamed:@"tab.png"] stretchableImageWithLeftCapWidth:8.0 topCapHeight:0.0];
        m_imageView.highlightedImage = [[UIImage imageNamed:@"tabSelected.png"] stretchableImageWithLeftCapWidth:8.0 topCapHeight:0.0];

        m_label = [[UILabel alloc] initWithFrame:CGRectZero];
        m_label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        m_label.backgroundColor = [UIColor clearColor];
        m_label.font = [UIFont boldSystemFontOfSize:12.0];
        m_label.textColor = unselectedColor;
        m_label.shadowOffset = CGSizeMake(0.0, 1.0);
        m_label.shadowColor = shadowColor; // Expected identifier before '[' token
        [m_label setShadowColor:shadowColor];

        m_closeButton = [[UIButton alloc] initWithFrame:CGRectMake(9.0, 1.0, CLOSEBUTTON_WIDTH, CLOSEBUTTON_HEIGHT)];
        [m_closeButton setBackgroundImage:[UIImage imageNamed:@"tabClose.png"] forState:UIControlStateNormal];
        [m_closeButton addTarget:self action:@selector(closeTab) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:m_imageView];
        [self addSubview:m_label];
        [self addSubview:m_closeButton];

        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

Any ideas?

Was it helpful?

Solution

#define shadowColor        [UIColor colorWithWhite:1.00 alpha:0.5]

is getting expanded on both sides of the assignment.

m_label.shadowColor = shadowColor;

evaluates to:

m_label.[UIColor colorWithWhite:1.00 alpha:0.5] = [UIColor colorWithWhite:1.00 alpha:0.5];

which is causing the compiler error. Use a different name for your #define:

#define SHADOW_COLOR        [UIColor colorWithWhite:1.00 alpha:0.5]

m_label.shadowColor = SHADOW_COLOR;

OTHER TIPS

Two things -- you need to add the following to your file:

#import <QuartzCore/QuartzCore.h>

The second is that you use a CGColorRef as the shadowColor, so you should be doing something like this:

m_label.layer.shadowColor = [[UIColor colorWithWhite:1.0 alpha:0.5] CGColor];

In an attempt to answer the original question now that I understand it a little better, it sounds like you're not including <UIKit/UILabel.h> somehow. It should already be included through <UIKit/UIKit.h> in your pre-compiled header file for your project.

Are you using the released 3.2 SDK and not one of the pre-release versions or the current beta of 4.0? If not, make sure you are using the correct SDK. Also make sure something didn't happen to your .pch file for your project. You should have an #import for <UIKit/UIKit.h> in it.

I did just test with the 3.2 SDK in my own project - I can use the shadowColor property on UILabel without the error that you are seeing. So it looks like something may have been broken on your end.

Make sure m_label is typed properly in your interface (that is, its class is UILabel). Property-dot syntax doesn't work if the compiler is using the wrong type, but the dynamic nature of Objective-C will allow you to send messages to objects which may not respond to them (and should also give a warning in that case). That's why -setShadowColor: is working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top