Universal class for Mac OS and iOS

I want to create a class for Mac OS and iOS app. Unfortunately, I cannot use NSColor for iOS and UIColor for Mac OS.

In fact, I have the following code:

#if os(iOS) func myFunc(color: UIColor?) { self.myFuncX(color) } #elseif os(OSX) func myFunc(color: NSColor?) { self.myFuncX(color) } #endif private func myFuncX(color: AnyObject?) { #if os(iOS) myColor = color as! UIColor #elseif os(OSX) myColor = color as! NSColor #endif } 

Is there a better way?

+5
source share
1 answer

You can use typealias for a color class, for example:

 #if os(iOS) typealias XColor = UIColor #elseif os(OSX) typealias XColor = NSColor #endif func myFunc(color: XColor?) { self.myFuncX(color) } 

The idea is to restrict conditional compilation to a type definition for XColor , and then use this type alias instead of UIColor or NSColor , as required for a particular system.

+9
source

All Articles