Search code examples
objective-cxcodeuicolor

Expected expression when using UIColorFromRGB


I have defined a standard UIColorFromRGB as:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];

When I am trying to use it as:

if (selectedRow == indexPath.row)
    cell.value.textColor = UIColorFromRGB(0x00C5FE);
else
    cell.value.textColor = [UIColor blackColor];

but I'm getting the error "Expected expression".
It works if I change it to:

if (self.selectedRow == indexPath.row) {
    cell.value.textColor = UIColorFromRGB(0x00C5FE);
}
else
    cell.value.textColor = [UIColor blackColor];

I'm trying to understand why is this?


Solution

  • Your macro has a trailing semicolon, so your first bit of code expands to:

    if (selectedRow == indexPath.row)
        cell.value.textColor = [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];;
    else
        cell.value.textColor = [UIColor blackColor];
    

    Note the second semicolon on line 2.

    Remove the semicolon from the end of your macro.