Skip to content

Instantly share code, notes, and snippets.

@rayfix
rayfix / CrashyViewController.swift
Created January 27, 2016 15:55
Trying to make a func visible to ObjC via protocol extension
import UIKit
protocol Tapper: class {
func install()
func foo()
}
extension Tapper where Self: UIViewController {
func install() {
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "foo"))
@rayfix
rayfix / fiindsize.swift
Created February 7, 2016 05:30
Getting the Size of a file or directory in Swift
//
// main.swift
// findsize
//
// Created by Ray Fix on 2/6/16.
// Copyright © 2016 Neko Labs. All rights reserved.
//
import Foundation
@rayfix
rayfix / pokemon.swift
Created March 6, 2016 13:31
Pokemon Type Erasure (take 2)
//: Second Type Erasure
import Foundation
protocol Pokemon {
typealias Power
func attack(power: Power)
}
private class _AnyPokemonBoxBase<Power>: Pokemon {
protocol Actionable {
func action()
}
enum Planets: Actionable {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
protocol PlanetsKind {}
@rayfix
rayfix / simplenetwork.swift
Created June 25, 2016 17:20
A simple network request using NSURLSession
import UIKit
import XCPlayground
var str = "Hello, playground"
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["foo": "bar"]
struct PokeCorrall {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: 0.0)
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
//: Playground - noun: a place where people can play
import UIKit
extension String {
var isBlank: Bool {
return trimmingCharacters(in: CharacterSet.whitespaces).isEmpty
}
}
@rayfix
rayfix / Cards.swift
Created October 16, 2016 17:58
Playing card abstraction
//: Cards
import Foundation
enum CardColor {
case red, black
}
enum CardSuit: CustomStringConvertible {
case heart, diamond, club, spade
/**
* Copyright (c) 2016 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
import Foundation
private enum RandomSource {
static private let file = fopen("/dev/urandom","r")!
static private let queue = DispatchQueue(label: "random")
static func get(count: Int) -> [Int8] {
let capacity = count + 1 // fgets adds null termination
var data = UnsafeMutablePointer<Int8>.allocate(capacity: capacity)
defer {
data.deallocate(capacity: capacity)