This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sumOf(numbers: Int...) -> Int { | |
var sum = 0 | |
for number in numbers { | |
sum += number | |
} | |
return sum | |
} | |
sumOf() | |
sumOf(42, 597, 12) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
func primeFactors(n: Int) -> Int[] { | |
if n == 1 { | |
return [1] | |
} | |
var result: Int[] = [] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
var componentsFormatter = NSDateComponentsFormatter() | |
componentsFormatter.unitsStyle = .Abbreviated | |
var components = NSDateComponents() | |
components.hour = 1 | |
components.day = 5 | |
components.year = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let 💩: Character = "💩" | |
func pooify(string: String) -> String { | |
var returnedString: String = "" | |
for c: Character in string { | |
returnedString.append(💩) | |
} | |
return returnedString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import Cocoa | |
// Variable names in Emoji! | |
var 💩 = "poo" | |
let 🐶 = "dog" | |
// A simple function. | |
let myName = "Jeff" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Security | |
import Foundation | |
let password = SecCreateSharedWebCredentialPassword().takeRetainedValue() as String | |
// Example output: 4w7-JLz-Q7S-9nk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func after(item: T) -> T? { | |
if let index = find(self, item) where index + 1 < count { | |
return self[index + 1] | |
} | |
return nil | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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(), |