Skip to content
/ X Public

Easier cross platform Mac & iOS development with Swift

License

Notifications You must be signed in to change notification settings

soffes/X

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

X

Version Swift Version Carthage compatible

Easier cross platform Mac and iOS development with Swift 5.1. X was abstracted out of Redacted and Whiskey.

Xcode 11.1 or higher is required.

Installation

Carthage is the recommended way to install X. Add the following to your Cartfile:

github "soffes/X"

Usage

X provides several typealiases to make dealing with AppKit/UIKit types that are similar. Currently, X includes:

Name UIKit AppKit
Color UIColor NSColor
ContentMode UIViewContentMode N/A
EdgeInsets UIEdgeInsets NSEdgeInsets
Font UIFont NSFont
GestureRecognizerStateType UIGestureRecognizerState NSGestureRecognizerState
Image UIImage NSImage
Screen UIScreen NSScreen
ViewType UIView NSView

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)

View

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`

Core Graphics

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.