0

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
5
  • Why not using an Extension, with +(NSColor *)myCustomBlackColor instead? Commented Jan 21, 2018 at 21:28
  • do you mean like my new edit? Commented Jan 21, 2018 at 21:39
  • and then I can call [Colours red]; ? Commented Jan 21, 2018 at 21:40
  • @Larme Too much Swift. You mean category, not extension. :) Commented Jan 21, 2018 at 23:02
  • @rmaddy Indeed, I meant "Category", that's why I also named myCustomBlackColor instead of black because of conflicts. Commented Jan 22, 2018 at 8:59

1 Answer 1

3

Newer Objective-C compilers support class level properties. If you look at the documentation for NSColor you will see that this is how all of the predefined colors are declared.

Colours.h:

@interface Colours : NSObject

@property (class, readonly) NSColor* black;
@property (class, readonly) NSColor* white;

@end

Colours.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];
}

@end

Import Colours.h where needed and then do:

NSColor *myColor = Colours.black;

Another option is to add a category to NSColor. This is nearly the same as what I show above but instead of declaring a class named Colours, you declare a category on NSColor. The important thing here is that your color names would need to be unique so you couldn't use blackColor or whiteColor since they already exist on NSColor.

NSColor+Mine.h:

@interface NSColor (Mine)

@property (class, readonly) NSColor* myBlack;
@property (class, readonly) NSColor* myWhite;

@end

NSColor+Mine.m:

@implementation NSColor (Mine)

+ (NSColor *)myBlack {
    return [NSColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1.0];
}

+ (NSColor *)myWhite {
    return [NSColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];
}

@end

Import NSColor+Mine.h where needed and then do:

NSColor *myColor = NSColor.myBlack;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.