Easier cross platform Mac and iOS development with Swift. X was abstracted out of Redacted and Whiskey.
This is a work in progress.
Carthage is the recommended way to install X. Add the following to your Cartfile:
github "soffes/X"
X provides several typealias
es to make dealing with AppKit/UIKit types that are similar. Currently, X includes:
Name | UIKit | AppKit |
---|---|---|
ColorType |
UIColor |
NSColor |
ContentMode |
UIViewContentMode |
N/A |
EdgeInsets |
UIEdgeInsets |
NSEdgeInsets |
FontType |
UIFont |
NSFont |
GestureRecognizerStateType |
UIGestureRecognizerState |
NSGestureRecognizerState |
ImageType |
UIImage |
NSImage |
ScreenType |
UIView |
NSView |
ViewType |
UIView |
NSView |
Note that all of these types end in Type
. This is consistent with the Swift standard library. There's also a type alias for all of these that omit the type like this:
public typealias Color = ColorType
This is handy because X can choose to later provide a subclass if some desired functionality isn't possible via extension
. This is the case for View
(see below).
If you wanted to use a color on both platforms, you could write something like this:
let blueColor = Color(red:0.298, green:0.757, blue:0.988, alpha: 1.0)
Note we use Color
instead of ColorType
since we are initializing one. If we were using it as a type, we should use ColorType
. Here's an example:
func changeColor(color: ColorType) {
// Do something
}
View
inherits from ViewType
(so either UIView
or NSView
) and adds some platform specific functionality. This makes methods like layoutSubviews
work on both platforms. The UIKit API is cleaner, so NSView has methods added in View
to make it behave more like UIView
.
Here's the current list of bridged methods that work on both:
var wantsLayer: Bool // On iOS, this doesn't do anything and always returns `true`.
func didMoveToWindow() // Bridged from `viewDidMoveToWindow`
func didMoveToSuperview() // Bridged from `didMoveToSuperview`
func layoutSubviews() // Bridged from `layout`
There are several extensions for CGPoint
, CGSize
, and CGRect
that help with converting to and from strings since UIKit and AppKit have different function names. The UIKit function names are aliased on Mac. There are also initializers and computed properties you can use instead of the functions.
Enjoy.