Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / Fonts.swift
Created Oct 6, 2016
Print all fonts in Swift 3
View Fonts.swift
UIFont.familyNames.forEach({ familyName in
let fontNames = UIFont.fontNames(forFamilyName: familyName)
print(familyName, fontNames)
})
@khanlou
khanlou / A - Usage.swift
Last active Jul 27, 2021
This ScrollView has a modifier called `onScroll`, which is updated when scrolls occur.
View A - Usage.swift
struct ContentView: View {
@State var scrollOffset: CGPoint = .zero
var body: some View {
ObservableScrollView {
Text("Hello, world!")
.foregroundColor(self.scrollOffset.y == 0 ? .blue : .red)
}
.onScroll { self.scrollOffset = $0 }
@khanlou
khanlou / CleanUpGit.md
Last active Jul 7, 2021
Clean up all merged branches
View CleanUpGit.md

To clean up git, run:

git remote prune origin; git branch --merged | grep -v '^* master$' | grep -v '^  master$' | xargs git branch -d

If it doesn't like that because it thinks the repo is not there, change the remote from http://github.com to http://khanlou@github.com

View SerialInputStream.swift
//Copyright 2019 Soroush Khanlou
//
//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:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE O
@khanlou
khanlou / SKSerialInputStream.h
Last active May 28, 2021
Partially adapted from AFMultipartBodyStream
View SKSerialInputStream.h
//
// SKSerialInputStream.h
// inputstream
//
// Created by Soroush Khanlou on 11/4/18.
// Copyright © 2018 Soroush Khanlou. All rights reserved.
//
#import <Foundation/Foundation.h>
@khanlou
khanlou / StateMachine.swift
Created Oct 21, 2016
A state machine where invalid transitions can't compile
View StateMachine.swift
protocol State {}
struct Transition<From: State, To: State> {
let transition: () -> To
}
struct Machine<CurrentState: State> {
var state: CurrentState
View Git-Hours.md

This is a quick way to approximate how many person-hours have put into a git repo.

git log --pretty=format:"%ae %ad" --date=format:'%Y-%m-%d %H' | uniq | wc -l

The --pretty=format gives you the author email and commit date, and the date is formatted to just include the year, month, day and hour, like so: 2020-12-22 16. You can add a grep before or after the unique to filter out by author. wc -l tells you the number of lines, which are uniqued, meaning if you had two commits in the same hour, those are filtered out.

@khanlou
khanlou / CanCan.swift
Created Feb 14, 2021
A version of Ruby's cancan for Swift.
View CanCan.swift
import Foundation
struct CanCan<Identity, Action: Equatable> {
var registry: [(String, (Identity, Any) -> [Action])] = []
mutating func register<T>(_ type: T.Type, _ block: @escaping (Identity, T) -> [Action]) {
self.registry.append((String(describing: type), { (identity: Identity, object: Any) -> [Action] in
guard let casted = object as? T else {
return []
View IndexedForEach.swift
//
// IndexedForEach.swift
//
//
// Created by Soroush Khanlou on 1/21/20.
//
//
import Foundation
import SwiftUI
View ScalableFonts.swift
import SwiftUI
enum ScalableFont {
case system(size: CGFloat, weight: Font.Weight = .regular, design: Font.Design = .default)
case custom(_ name: String, size: CGFloat)
var size: CGFloat {
switch self {