Skip to content

Instantly share code, notes, and snippets.

@jon-cotton
jon-cotton / StateStore.swift
Last active June 23, 2016 16:46
Implementation of a Redux/Flux like store with a single AppState in Swift 3.0. Makes use of some of the concurrency techniques demonstrated in the WWDC 2016 talk 'Concurrent Programming With GCD in Swift 3' https://developer.apple.com/videos/play/wwdc2016/720/
//: Swift 3 State Store
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
protocol State {
var counter: Int { get }
}
//: Playground - noun: a place where people can play
import Foundation
enum PersonError: ErrorProtocol {
case doesNotExistInDataSource
case emailNotInPersonalData
}
struct Person {
#!/usr/bin/swift
import Foundation
enum Device: String {
case phone
case tablet
var videoSize: Int {
switch self {
@jon-cotton
jon-cotton / Validation.swift
Last active May 31, 2016 09:30
Basic validation 'engine' for Swift, allows you to quickly test a group of UITextFields against either basic built in validation rules or custom regex patterns and get back a group of specfic errors as to what didn't match/validate. Copy and paste the raw contents into a Swift playground to try it out for yourself.
//: Playground - noun: a place where people can play
import UIKit
protocol Validator {
associatedtype T
func isValid(value: T) throws -> Bool
}
@jon-cotton
jon-cotton / printable-string-enums.swift
Last active May 12, 2016 07:59
Make all string based enums String convertible using the rawValue
extension CustomStringConvertible where Self: RawRepresentible, Self.RawValue == String {
var description: String {
return rawValue
}
}
extension CustomDebugStringConvertible where Self: RawRepresentible, Self.RawValue == String {
var debugDescription: String {
return "\(self)"
}
#!/usr/bin/swift
import Foundation
// MARK:- Functions
func runShellCommand(cmd: String, env: [String : String]? = nil) -> (output: [String], error: [String], exitCode: Int32) {
var output : [String] = []
var error : [String] = []
enum AlertsStatusUpdateAction: ActionCreatingAction {
case requestUpdate(AlertsService)
case updateFinishedWithAlerts([InAppAlert])
case updateFinishedWithError(AlertsError)
func execute(state: AppState) -> AppState {
var mutableState = state
switch self {
case .requestUpdate(_):
import Foundation
import XCPlayground
/*:
# Reflux
*/
//: Interfaces
protocol State {
init()
var userState: UserState {get}
//: Adventures in generics
import Foundation
protocol MyBaseProtocol {
func didSomething()
func didAnotherThing()
}
protocol MyDataProtocol : MyBaseProtocol {
// Copy and Paste into a Swift playground
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
// Data Source (anything that implements subscripting for accessing data)
//protocol MapDataProviding {
// typealias Key: Hashable