Skip to content

Instantly share code, notes, and snippets.

@rayfix
rayfix / AnchorPointDemo.swift
Created May 2, 2020 18:46
A demonstration of using anchor points in SwiftUI
import SwiftUI
struct ContentView: View {
@State private var scale: CGFloat = 1
@State private var angle = Angle(radians: 0)
@State private var color = UIColor.blue
var body: some View {
Image(systemName: "heart.fill")
@rayfix
rayfix / KeyPathDemo.swift
Created April 6, 2020 06:49
Playing around with KeyPaths in Swift
struct Paragraph {
var words: String
}
struct Chapter {
var title: String
var content: [Paragraph]
}
extension UIImage {
enum HEICError: Error {
case heicNotSupported
case cgImageMissing
case couldNotFinalize
}
static var isHEICSupported: Bool = {
let data = NSMutableData()
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}",
@rayfix
rayfix / scientific.swift
Last active April 23, 2019 16:04
Scientific Notation
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}",
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)
@rayfix
rayfix / result.swift
Created April 23, 2019 16:01
Result type
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
@rayfix
rayfix / unknown.swift
Created April 23, 2019 16:00
Unknown cases
enum Coin {
case heads
case tails
var isHeads: Bool {
switch self {
case .heads:
return true
case .tails:
return false
func compute(_ a: inout Int, _ b: inout Int) {
b = 100
a = 10
}
var x = 0
var y = 0
compute(&x, &y)
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
}