Skip to content

Instantly share code, notes, and snippets.

View JosephDuffy's full-sized avatar
🐒
Code monkey

Joseph Duffy JosephDuffy

🐒
Code monkey
View GitHub Profile
@nicklockwood
nicklockwood / CodableVersioning.swift
Last active January 29, 2024 11:31
Example demonstrating how to use versioning for Codable structs
// This gist demonstrates how you can implement versioning for a Codable struct to support loading
// old serialized data after changing the structure. Notable features of this solution:
//
// * No need to make new properties optional, or to perform post-processing on the struct after
// loading in ordeer to populate missing values
// * No need to change the call site - from the outside this struct behaves just the same
// as if we had implemented codable directly in the normal way.
// * Versioning can be applied individually to parents or leaves in a larger tree of
// structs without affecting the other elements
// * This approach will work even if the original struct was not designed with versioning in mind
@siraben
siraben / autocollect.sh
Last active February 9, 2021 18:59
macOS script to collect and dedup MAC addresses from a network
#!/bin/bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2018 Ben Siraphob <bensiraphob@gmail.com>
command -v gshuf >/dev/null 2>&1 || { echo >&2 "gshuf is required but is not installed. Run \"brew install coreutils\"."; control_c; }
command -v spoof-mac >/dev/null 2>&1 || { echo >&2 "spoof-mac is required but is not installed. Run \"brew install spoof-mac\"."; control_c; }
# Colors
RED='\033[0;31m'
NC='\033[0m'
@DougGregor
DougGregor / parallel_map.swift
Created December 24, 2020 01:10
Swift async/await implementation of a parallel map
extension Collection {
func parallelMap<T>(
parallelism requestedParallelism: Int? = nil,
_ transform: @escaping (Element) async throws -> T
) async throws -> [T] {
let defaultParallelism = 2
let parallelism = requestedParallelism ?? defaultParallelism
let n = self.count
if n == 0 {
@shaps80
shaps80 / ActivityView.md
Last active December 3, 2022 15:36
A complete SwiftUI UIActivityViewController implementation.

Moved

This is now available in a dedicated package: ActivityView

Alternatively my SwiftUI Backports now includes a more complete implementation of ShareLink that's also more performant.

@JosephDuffy
JosephDuffy / Get PNG from UIImageView.md
Last active September 11, 2019 11:01
Extracting images from a UIImageView
  1. Find UIImageView address (e.g. via view debugger)
  2. In the console: po [UIImagePNGRepresentation((UIImage *)[_address_ image]) base64EncodedStringWithOptions:0]
  3. Copy base 64 output
  4. In a terminal: pbpaste | base64 -D > image.png
@JosephDuffy
JosephDuffy / README.md
Last active February 20, 2019 16:23
fish function that uses bundler version of fastlane when available

To install add to ~/.config/fish/functions/. This can be done manually or via curl -o ~/.config/fish/functions/fastlane.fish https://gist.githubusercontent.com/JosephDuffy/596821c21b6810dd0c82639b1c6b8a64/raw/fastlane.fish

This function has 2 primary advantages:

  • No need to remember to type bundle exec before fastlane commands
  • Autocomplete will work when installed via fastlane enable_auto_complete

Disclamer: My fish scripting skills aren't exactly 💯. If you have any suggestions for improvements please tweet them at me!

import Foundation
struct Partial<Wrapped>: CustomStringConvertible, CustomDebugStringConvertible {
enum Error<ValueType>: Swift.Error {
case missingKey(KeyPath<Wrapped, ValueType>)
case invalidValueType(key: KeyPath<Wrapped, ValueType>, actualValue: Any)
}
private var values: [PartialKeyPath<Wrapped>: Any?] = [:]
@JosephDuffy
JosephDuffy / ExampleViewController.swift
Last active November 30, 2022 18:28
Animates changing the brightness of a `UIScreen`
import UIKit
final class ExampleViewController: UViewController {
private var usersBrightness = UIScreen.main.brightness
private var willEnterForegroundWasCalled = false
private var viewWillDisappearWasCalled = false
override func viewDidLoad() {
@JosephDuffy
JosephDuffy / fastlane.fish
Last active March 16, 2017 19:38
A simple fish script to provide tab completion of fastlane lanes
# Author: Joseph Duffy
# License: MIT
# Notes: Place this script in `~/.config/fish/completions/` to enable Fastlane lane completion
# URL: https://gist.github.com/JosephDuffy/e40fc115f849555c5ef8453d3ece1c10
# This function was taken from https://github.com/Carthage/Carthage/blob/master/Source/Scripts/carthage-fish-completion
function __fish_prog_needs_subcommand
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'fastlane' ]
return 0
@magnusdahlstrand
magnusdahlstrand / gist:1923948
Created February 27, 2012 13:58
Remove all ._* and .AppleDouble-files
Remove ._*- and .AppleDouble-files from folders.
Adapted from Torkel's comment on http://hints.macworld.com/article.php?story=20021118060850652
Cred to hiber for suggesting adding -print0/-0 to solve whitespace issue.
See all ._*-files:
find . -name '._*' | more
Remove all ._*-files:
find . -name '._*' -print0 | xargs -0 rm