Skip to content

crossroadlabs/Foundation3

Repository files navigation

Foundation3

Foundation3

🐧 linux: ready ![🐤 3.0: ready](https://img.shields.io/badge/🐤 3.0-ready-reallyed.svg) Build Status Carthage compatible ![Platform OS X | iOS | tvOS | watchOS | Linux](https://img.shields.io/badge/platform-OS X | iOS | tvOS | watchOS | Linux-orange.svg) ![Swift version](https://img.shields.io/badge/Swift-2.2 | 3.0-blue.svg) ![GitHub license](https://img.shields.io/badge/license-Apache 2.0-lightgrey.svg) GitHub release

Swift Foundation Shims that mimic Swift 3.0 APIs. For thous who wants to maintain Swift multiple versions compatibility

Goals

Foundation3 library was mainly introduced to fulfill the needs of Swift Express - web application server side framework for Swift. Now it's a part of Crossroad Labs Foundation.

Still we hope it will be useful for everybody else.

Bother less with #if swift(>=3.0) ;)

Getting started

Installation

Add the following dependency to your Package.swift:

.Package(url: "https://github.com/crossroadlabs/Foundation3.git", majorVersion: 0)

Run swift build and build your app. Package manager is supported on OS X, but it's still recommended to be used on Linux only.

Add the following to your Cartfile:

github "crossroadlabs/Foundation3"

Run carthage update and follow the steps as described in Carthage's README.

Add the following to your Podfile:

pod 'Foundation3'

Make sure that you are integrating your dependencies using frameworks: add use_frameworks! to your Podfile. Then run pod install.

Examples

Introduction:

OK, let's say you want to get NSData from String. There is a standard method for it in Foundation. Still, the signatures are

func dataUsingEncoding(encoding: NSStringEncoding, allowLossyConversion: Bool = default) -> NSData?

and

func data(usingEncoding encoding: NSStringEncoding, allowLossyConversion: Bool = default) -> NSData?

for Swift 2.2 and Swift 3.0 respectively. And this is in Mac only (in Linux it's even more of a mess).

The problem is that if you would like to maintain compatibility for several versions (which you better do, especially if you are a Framework developer as Swift is evolving and changing amazingly fast) your code will get all like this:

#if swift(>=3.0)
let data = string.data(usingEncoding: encoding, allowLossyConversion: allowLossyConversion)
#else
let data = string.dataUsingEncoding(encoding, allowLossyConversion: allowLossyConversion)
#endif

which obviously is not that pleasant.

The solution we have chosen for Swift Express: The most advanced Swift Web Application Framework is to use the latest APIs everywhere (3.0 at the time of writing). Thus, we have created Boilerplate, Foundation3 and XCTest3 which mimic Swift 3.0 APIs where they are not available.

Effectively this makes the following code to work the same way everywhere where at least Swift 2.2 is available:

import Foundation3

let data = string.data(usingEncoding: encoding, allowLossyConversion: allowLossyConversion)

This is how we do Express to be Swift 2.2 and 3.0 compatible at the same time.

Absent API?

OK, guys. We wrap Foundation APIs as we encounter them. Want more? We are glad to accept contributions. Let's make Swift 3.0 compatibility layer together.

Keep your code clean ;)

Contributing

To get started, sign the Contributor License Agreement.