Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
SlaunchaMan / average.swift
Created June 3, 2014 18:03
Swift Averages
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)
@SlaunchaMan
SlaunchaMan / PrimeFactors.swift
Created June 3, 2014 21:41
A quick Prime Factors kata in Swift
import XCTest
func primeFactors(n: Int) -> Int[] {
if n == 1 {
return [1]
}
var result: Int[] = []
@SlaunchaMan
SlaunchaMan / NewFormatters.playground
Created August 8, 2014 19:07
New Formatters in iOS 8
import UIKit
var componentsFormatter = NSDateComponentsFormatter()
componentsFormatter.unitsStyle = .Abbreviated
var components = NSDateComponents()
components.hour = 1
components.day = 5
components.year = 1
@SlaunchaMan
SlaunchaMan / 💩.swift
Last active August 29, 2015 14:05
Swift is silly
let 💩: Character = "💩"
func pooify(string: String) -> String {
var returnedString: String = ""
for c: Character in string {
returnedString.append(💩)
}
return returnedString
@SlaunchaMan
SlaunchaMan / A Swift Introduction.swift
Last active August 29, 2015 14:06
A Swift Introduction
// Playground - noun: a place where people can play
import Cocoa
// Variable names in Emoji!
var 💩 = "poo"
let 🐶 = "dog"
// A simple function.
let myName = "Jeff"
@SlaunchaMan
SlaunchaMan / OptionalMap.swift
Last active August 29, 2015 14:21
Optional Map
//: Playground - noun: a place where people can play
import Cocoa
/* If you’re doing something like parsing JSON, and you have an array of
dictionaries, optionalMap is nice to only parse *valid* model objects out of
the dictionary. It allows you to be type-safe while still discarding junk
data. */
extension Array {
func optionalMap<U>(transform: (T) -> U?) -> [U] {
@SlaunchaMan
SlaunchaMan / GeneratePasswords.swift
Last active August 29, 2015 14:22
Using the Security Framework to Generate Passwords
import Security
import Foundation
let password = SecCreateSharedWebCredentialPassword().takeRetainedValue() as String
// Example output: 4w7-JLz-Q7S-9nk
@SlaunchaMan
SlaunchaMan / After.swift
Created June 17, 2015 18:38
Getting the Next Element in a Swift Array
extension Array {
func after(item: T) -> T? {
if let index = find(self, item) where index + 1 < count {
return self[index + 1]
}
return nil
}
}
@SlaunchaMan
SlaunchaMan / ItemsOfKind.swift
Created June 29, 2015 18:08
Returns an array containing the given array’s items of type U.
extension Array {
func optionalMap<U>(transform: (T) -> U?) -> [U] {
return map(transform).filter { $0 != nil }.map { $0! }
}
func itemsOfKind<U>(kind: U.Type) -> [U] {
return optionalMap { $0 as? U }
}
}
@SlaunchaMan
SlaunchaMan / DispatchAfter.swift
Created July 28, 2015 14:19
Swift-friendly dispatch_after()
/**
Shadows the system’s `dispatch_after()` function with a friendlier syntax for Swift.
Performs the `block` on `queue` after `delay` seconds.
:param: delay The delay (in seconds). Defaults to 0.1 seconds.
:param: queue The dispatch queue to perform the work on. Defaults to the main queue.
:param: block The block to execute after the delay.
*/
public func dispatch_after(delayInSeconds delay: NSTimeInterval = 0.1,
queue: dispatch_queue_t = dispatch_get_main_queue(),