Skip to content

Instantly share code, notes, and snippets.

View daneden's full-sized avatar

Daniel Eden daneden

View GitHub Profile
daneden / ContentView.swift
Created January 29, 2024 14:41
SwiftUI app showing how to prevent multiple `WindowGroup` windows
// ContentView.swift
// WindowExample
// Created by Daniel Eden on 29/01/2024.
import SwiftUI
struct ContentView: View {
struct Fields {
var userFields: Set<User.Fields>?
var tweetFields: Set<Tweet.Fields>?
protocol Fetcher {
// Is there a way to do something like this, where I can specify that the `Fields`
// struct must contain only these member types?
func getTweet(fields: Fields<Tweet.Fields, User.Fields>? = nil) async -> Tweet
func getUser(fields: Fields<User.Fields>? = nil) async -> User
struct User: Codable, Identifiable {
typealias ID = String
let id: ID
let name: String
let username: String
// Assume that items can be optionally set in getUser
var items: UserItems?

I’m trying to decode two slightly different JSON API responses to the same Codable type, Account. Mapping userResponse.json’s uid to id is trivial, but extracting the nested user is stumping me.

Any suggestions would be greatly appreciated!

// Color.extension.swift
// Zeitgeist
// Created by Daniel Eden on 30/12/2020.
// Copyright © 2020 Daniel Eden. All rights reserved.
import Foundation
import SwiftUI
// $0 is web inspector's reference to the current element
// run a regex to get the values for r, g, b, and optionally a
.match(/rgba?\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})(, ?([0-9.]{1,3}))?\)/)
// filter out the regex results we don't care about
.filter((e, i) => {
switch (i) {
case 1: // red
case 2: // green
case 3: // blue
# Checkout master branch
git checkout master
# Rename "master" to "main"
git branch -m master main
# Unset the current upstream branch
git branch --unset-upstream
import SwiftUI
import Combine
struct HMS {
var h: Int
var m: Int
var s: Int
struct ContentView: View {

git clone and cd function

A simple little alias function that lets you clone and cd into a GitHub repo in one command.


Add to your zsh or bash config (usually ~/.bash_profile or ~/.zshrc) and open a new terminal to be able to use the function.


gcd [github_repo_owner/repo_name]
// I learned in this post (
// that arguments passed to a function run before the function itself:
* // This runs second
* outerFunction(
* // This runs first
* innerFunction()
* )
// This makes sense. But I wondered what it looked like in practice.