Skip to content

Instantly share code, notes, and snippets.

View UIImage+heicData.swift
extension UIImage {
enum HEICError: Error {
case heicNotSupported
case cgImageMissing
case couldNotFinalize
}
static var isHEICSupported: Bool = {
let data = NSMutableData()
View superMap.swift
private static let toSuper: [Character: String] = ["0": "\u{2070}",
"1": "\u{00B9}",
"2": "\u{00B2}",
"3": "\u{00B3}",
"4": "\u{2074}",
"5": "\u{2075}",
"6": "\u{2076}",
"7": "\u{2077}",
"8": "\u{2078}",
"9": "\u{2079}",
View CodableRange.swift
struct Dice: Codable {
var range: Range<Int>
init<R: RangeExpression>(range expr: R) where R.Bound == Int {
range = expr.relative(to: Range<Int>(Int.min...Int.max-1))
}
func roll() -> Int {
return Int.random(in: range)
View result.swift
enum Result<Success, Failure: Error> {
case success(Success)
case failure(Failure)
func get() throws -> Success {
switch self {
case .success(let success):
return success
case .failure(let failure):
throw failure
View unknown.swift
enum Coin {
case heads
case tails
var isHeads: Bool {
switch self {
case .heads:
return true
case .tails:
return false
View exclusivity.swift
func compute(_ a: inout Int, _ b: inout Int) {
b = 100
a = 10
}
var x = 0
var y = 0
compute(&x, &y)
@rayfix
rayfix / scientific.swift
Last active Apr 23, 2019
Scientific Notation
View scientific.swift
import Foundation
extension String.StringInterpolation {
private static let toSuper: [Character: String] = ["0": "\u{2070}",
"1": "\u{00B9}",
"2": "\u{00B2}",
"3": "\u{00B3}",
"4": "\u{2074}",
"5": "\u{2075}",
View RobotImperative.swift
enum Direction {
case north, east, south, west
var left: Direction {
switch self {
case .north: return .west
case .west: return .south
case .south: return .east
case .east: return .north
}
View Robot.swift
enum Direction {
case north, east, south, west
var left: Direction {
switch self {
case .north: return .west
case .west: return .south
case .south: return .east
case .east: return .north
}
View pm-lesson3.swift
// The contingency table contains the support counts for each item
// in comparision with one another.
struct ContingencyTable {
var bc: Int // B and C
var b_c: Int // B and not C
var _bc: Int // not B and C
var _b_c: Int // not B and not C