I have a load of variables that I want to be accessed from multiple classes in objective-c.
For example I have a list of colours. Ideally I would just have a Colours.h file and do something like this:
@interface Colours : NSObject
@property (readonly) NSColor* black = [NSColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1.0];
@property (readonly) NSColor* white = [NSColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];
@property (readonly) NSColor* red = [NSColor colorWithRed:0.74 green:0.13 blue:0.13 alpha:1.0];
@property (readonly) NSColor* grey = [NSColor colorWithRed:0.43 green:0.43 blue:0.43 alpha:1.0];
@property (readonly) NSColor* boarder = [NSColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
@property (readonly) NSColor* offwhite = [NSColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
@end
and then import the Colours.h in each of my classes and call Colours.boarder but this is not possible.
So should I do something like this with a .m?
#import "Colours.h"
@implementation Colours
-(id)init{
_black = [NSColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1.0];
_white = [NSColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];
_red = [NSColor colorWithRed:0.74 green:0.13 blue:0.13 alpha:1.0];
_grey = [NSColor colorWithRed:0.43 green:0.43 blue:0.43 alpha:1.0];
_boarder = [NSColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
_offwhite = [NSColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
}
@end
This seems a little overkill as I would then need to init the class?
I am also worried that I have class A which imports class B and B imports class C and all three classes need the colours. Is it bad to use the same #import for each class or is there a more efficient way to do this?
another technique?
.h
#import <Cocoa/Cocoa.h>
@interface Colours: NSObject
+(NSColor *)black;
+(NSColor *)white;
+(NSColor *)red;
+(NSColor *)grey;
+(NSColor *)boarder;
+(NSColor *)offwhite;
@end
.m
@implementation Colours
+(NSColor *)black{
return [NSColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1.0];
}
+(NSColor *)white{
return [NSColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];
}
+(NSColor *)red{
return [NSColor colorWithRed:0.74 green:0.13 blue:0.13 alpha:1.0];
}
+(NSColor *)grey{
return [NSColor colorWithRed:0.43 green:0.43 blue:0.43 alpha:1.0];
}
+(NSColor *)boarder{
return [NSColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
+(NSColor *)offwhite{
return [NSColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
}
@end
+(NSColor *)myCustomBlackColorinstead?[Colours red];?myCustomBlackColorinstead ofblackbecause of conflicts.