Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Hudson twostraws

View GitHub Profile
View FetchAndDecode.swift
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type = T.self,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
@twostraws
twostraws / gasp.swift
Created Mar 13, 2020
A quick clone of the watchOS Breathe UI, for Swift Over Coffee
View gasp.swift
//
// Gasp – a quick clone of the watchOS Breathe UI, for Swift Over Coffee
// https://github.com/twostraws/SwiftOverCoffee
//
import SwiftUI
struct ContentView: View {
@State private var flowerOut = false
var body: some View {
@twostraws
twostraws / arrays.swift
Created Jan 28, 2020
A microbenchmark for a potential Swift standard library addition
View arrays.swift
//
// FOR FOLKS STUMBLING ACROSS THIS LATER
// This is an example of code that allows us to create arrays by calling a function a fixed number of times.
// In my example below I'm just sending in 5 because I don't want to cloud the benchmark with generating
// a random number or similar, but with this change you can replace the 5 with `Int.random(in: 1...10)` to
// get an array of random numbers.
//
// HERE'S THE STDLIB ADDITION TO MAKE THIS WORK:
// @inlinable
// @_semantics("array.init")
View sundell.swift
import Foundation
let sundelL = { (t: Int) in return -(~t) }
let sundeLl = { return sundelL(sundelL($0)) }
let sundEll = { return sundeLl(sundeLl($0)) }
let sunDell = { return sundEll(sundEll($0)) }
let suNdell = { return sunDell(sunDell($0)) }
let sUndell = { return suNdell(suNdell($0)) }
let Sundell = { return sUndell(sUndell($0)) }
let sundell = { (t: Int) -> Int in print(UnicodeScalar(t)!, terminator: ""); return 0 }
@twostraws
twostraws / type-layout-fuzzer.swift
Created Sep 21, 2017
A Swift 4 port of Joe Groff's type-layout-fuzzer.py
View type-layout-fuzzer.swift
//
// type-layout-fuzzer.swift
// Paul Hudson / @twostraws / www.hackingwithswift.com
//
// This is a Swift 4 port of Joe Groff's type-layout-fuzzer.py.
// I haven't attempted to Swiftify the code – it's pretty
// much a line-by-line port.
//
// License: public domain / CC0 / seriously, do whatever
// you want with it.
@twostraws
twostraws / TopSekritPokemonGoSourceCode.swift
Created Jul 17, 2016
Totally Top Sekrit Pokémon Go Source Code
View TopSekritPokemonGoSourceCode.swift
//
// PokemonGoViewController.swift
// Totally Top Sekrit Pokémon Go Source Code
//
// Created by Niantic on 07/01/2016.
// Copyright © 2016 Niantic rights reserved.
//
import UIKit
View gist:3d673d4eba36de173f6f
//
// loveWins(): a simple function that accepts a UIImage and
// returns the same image blended with the rainbow flag
// of the LGBT pride movement.
//
// This is released for pedagogical reasons (I've tried to make
// the code as easy to follow as possible!) but you're welcome
// to use it for any purpose – consider the code yours.
//
// If you're using Xcode 7 / Swift 2, you need to make a tiny
View Simple Scrolling UIStackView
//
// SimpleScrollingStack.swift
// A super-simple demo of a scrolling UIStackView in iOS 9
//
// Created by Paul Hudson on 10/06/2015.
// Learn Swift at www.hackingwithswift.com
// @twostraws
//
import UIKit