I have a .h file like this:
#import <UIKit/UIKit.h>
#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]
long colorPresetHex = 0xdedede; // this is OK.
UIColor *colorPreset = UIColorFromRGB (colorPresetHex); // Error
This .h will be called from many places that requires this 'standard'. The problem is there's an error occurred on the line UIColor *colorPreset = UIColorFromRGB (colorPresetHex);
said "initializer element is not a compile-time constant". Now I know why this is happened. What I want to do is how to work around this issue so that I can import this header file on any project and can immediately use the colorPreset
variable. I've tried to put the initializer on .m, but it does not work either:
.h
#import <UIKit/UIKit.h>
#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]
long colorPresetHex = 0xdedede;
UIColor *colorPreset;
.m
#import ".h"
UIColor *colorPreset = UIColorFromRGB (colorPresetHex); // error: Redefinition of 'colorPreset'
colorPreset = UIColorFromRGB (colorPresetHex); // error: Redefinition of 'colorPreset' with different type: 'int' vs 'UIColor *__strong'
How can I work around this? I'd like to keep the initializer on this .h or .m, because I might copy these two files to another project, and if the initializer is in different class, that might make everything messy. Thanks.