Skip to content

Instantly share code, notes, and snippets.

//
// SemanticVersionNumber.swift
//
// Created by Jon Cotton on 31/08/2015.
//
import Foundation
public struct SemanticVersionNumber {
let major: Int
// Copy and Paste into a Swift playground
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
typealias MappingDataSource = [String : AnyObject]
// Mappable
protocol Mappable {
// 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
//: Adventures in generics
import Foundation
protocol MyBaseProtocol {
func didSomething()
func didAnotherThing()
}
protocol MyDataProtocol : MyBaseProtocol {
import Foundation
import XCPlayground
/*:
# Reflux
*/
//: Interfaces
protocol State {
init()
var userState: UserState {get}
enum AlertsStatusUpdateAction: ActionCreatingAction {
case requestUpdate(AlertsService)
case updateFinishedWithAlerts([InAppAlert])
case updateFinishedWithError(AlertsError)
func execute(state: AppState) -> AppState {
var mutableState = state
switch self {
case .requestUpdate(_):
#!/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] = []
@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)"
}
@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
}
#!/usr/bin/swift
import Foundation
enum Device: String {
case phone
case tablet
var videoSize: Int {
switch self {