Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / YouDeserveNiceErrors.swift
Last active October 18, 2020 03:00
Nicer descriptions for DecodingErrors
extension DecodingError.Context {
var pathDescription: String {
pathDescription(for: codingPath)
}
func path(including final: CodingKey) -> String {
pathDescription(for: codingPath + [final])
}
private func pathDescription(for path: [CodingKey]) -> String {

This is designed to look like sheet(item:content:):

func sheet<Item, Content>(item: Binding<Item?>, content: @escaping (Item) -> Content) -> some View

But for pushing views instead of presenting them as sheets.

To use it, create a @State variable for your selected item:

@State var selectedPerson: Person?
import Foundation
import MapKit
extension MKMapPoint {
static var nyc: MKMapPoint {
return MKMapPoint(.nyc)
}
}
extension MKMapRect {
struct Statistics {
private(set) var count: Double = 0
private(set) var sum: Double = 0
private(set) var sumOfSquares: Double = 0
var average: Double {
sum / count
}
@khanlou
khanlou / Rope.swift
Last active July 6, 2020 23:46
A little...ropes course
struct Rope {
enum Node: ExpressibleByStringLiteral {
case leaf(String, length: Int)
indirect case branch(left: Node, right: Node?, length: Int)
public init(stringLiteral value: StaticString) {
let string = "\(value)"
self = .leaf(string, length: string.count)
}
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 {
//
// Spinner.swift
// spinner
//
// Created by Soroush Khanlou on 5/29/20.
// Copyright © 2020 Soroush Khanlou. All rights reserved.
//
import SwiftUI
@khanlou
khanlou / A - Usage.swift
Last active October 18, 2022 04:32
This ScrollView has a modifier called `onScroll`, which is updated when scrolls occur.
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 }
tell application "Music"
-- Get list of albums, the fast way...
set albumsWithDups to (album of every track)
set albumsNames to my removeDuplicates(albumsWithDups)
-- Check each album
repeat with currentAlbum in albumsNames
set albumSongs to (every track of library playlist 1 whose album is currentAlbum)
//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