Skip to content

Instantly share code, notes, and snippets.

typealias BOOLAEN = Bool
typealias LIKEON68K = UInt16
typealias STRING = String
typealias STRINGINDEX = String.Index
typealias BOOL = Bool
typealias UNICODESCALER = UnicodeScalar
typealias GONADS = String
typealias NUMBERSWITHDOTS = Double
typealias CH = Character
typealias AMANA = Range<STRINGINDEX>

CocoaPods only supports Swift on OS X 10.9 and newer, and iOS 8 and newer.

Here's why:

  • Swift is supported on OS X 10.9 / iOS 7 and newer, as stated by Apple numerous times.
  • There is no support for building static archives with Swift.
  • Dynamic frameworks are supported on all versions of OS X.
  • Dynamic frameworks are unsupported on iOS versions prior to 8:

> ld: warning: embedded dylibs/frameworks only run on iOS 8 or later.

@getaaron
getaaron / take_while.swift
Last active August 29, 2015 14:22
Ruby's take_while ported using a Swift 2.0 protocol extension
var source = [1, 2, 3, 4, 5, 0]
extension CollectionType {
func takeWhile(@noescape condition: (Self.Generator.Element) -> Bool) -> [Self.Generator.Element] {
var returnArray : [Self.Generator.Element] = []
for x in self {
guard condition(x) else { break }
returnArray.append(x)
@getaaron
getaaron / SaveContexts.swift
Last active August 29, 2015 14:25
Attempts to call save() on each NSManagedObjectContext in a given array. Rolls back saved changes and throws the provided error if a save fails.
/* Attempts to call save() on each NSManagedObjectContext in a given array. Rolls back saved changes and throws the provided error if a save fails. */
func saveContexts(contexts: [NSManagedObjectContext]) throws {
var successfulContextSaves = [NSManagedObjectContext]()
for context in contexts where context.hasChanges {
do {
try context.save()
successfulContextSaves += [context]
func mean(numbers: [Double]) -> Double {
return numbers.reduce(0, combine: +) / Double(numbers.count)
}
func mean(numbers: Double...) -> Double {
return mean(numbers)
}
mean(1, 2, 3, 4, 5) // 3
mean([1,2,3,4,5]) // 3
func measure(block : () -> ()) -> NSTimeInterval {
var averages = [NSTimeInterval]()
for _ in 1...1000 {
let date = NSDate()
block()
averages.append(NSDate().timeIntervalSinceDate(date))
}
return averages.reduce(0, combine: +) / Double(averages.count)
}
// determines the state of a given Connect Four board
// http://www.codewars.com/kata/529962509ce9545c76000afa/train/javascript
function connectFour(board) {
horizontal = straight(board, true);
if (horizontal) return horizontal;
vertical = straight(board, false);
if (vertical) return vertical;
@getaaron
getaaron / dub.swift
Last active January 31, 2016 03:46
A Swift extension to split a string into an array of strings
extension String {
func split(string: String) -> [String] {
guard !string.characters.isEmpty else { return [string] }
var strings: [String] = []
let targetDistance = string.characters.startIndex.distanceTo(string.characters.endIndex)
var currentIndex = self.startIndex
var currentView = String.CharacterView()
@getaaron
getaaron / markdown-anchor.swift
Created February 5, 2016 22:06
Generates RedCarpet-compatible anchor text
import Foundation
extension Character {
var invalid: Bool {
let invalid = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters)
return invalid.contains(self)
}
var isAscii: Bool {
guard let number = String(self).utf16.first else { return false }
@getaaron
getaaron / Fibonacci.swift
Last active January 29, 2017 21:23
Some implementations of a Fibonacci method in Swift
// The correct numbers
let expectedSequence = [1, 1, 2, 3, 5, 8, 13, 21, 34]
// Iterative
func digit(index: Int) -> Int {
var digits = [1, 1]
for i in (2...index) {