Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically choose between NSColor and UIColor for the correct build system? (Using a #define, or something)

I'm trying to make a NSObject subclass that will have a lot of methods that return colors, so I wanna return UIColor if I'm building for iOS or NSColor if I'm building for OS X.
This is kind of a pseudo-code of what the expected behaviour should be:

#define COLOR #if TARGET_OS_IPHONE UIColor #elif TARGET_OS_MAC NSColor #endif

+ (COLOR *)makeMeColorful;

Is it possible to do something like this instead of making 2 methods for each of my object's method (one for iOS and another for OS X)?

like image 247
Pedro Vieira Avatar asked Feb 14 '14 19:02

Pedro Vieira


People also ask

What is Nscolor?

An object that stores color data and sometimes opacity (alpha value).

How do I color in Swiftui?

For adding a color set, you go to the Assets Folder -> Click "+" button on bottom left corner -> "Add Color Set". You can customize the color for a particular color scheme or can have the same color for both modes.


1 Answers

This is absolutely doable. SKColor from SpriteKit for example, is defined like:

#if TARGET_OS_IPHONE
#define SKColor UIColor
#else
#define SKColor NSColor
#endif

And then utilized like this:

SKColor *color = [SKColor colorWithHue:0.5 saturation:1.0 brightness:1.0 alpha:1.0];

This simply takes advantage of the fact that UIColor and NSColor share some of their class methods.

like image 166
Mick MacCallum Avatar answered Nov 16 '22 04:11

Mick MacCallum