Skip to content

Instantly share code, notes, and snippets.

View xsleonard's full-sized avatar

Steve Leonard xsleonard

View GitHub Profile
@finestructure
finestructure / OrderedDict.swift
Last active September 13, 2020 14:20
Swift ordered dictionary
import Foundation
struct OrderedDictionary<K: Hashable, V> {
var keys: [K] = []
var dict: [K: V] = [:]
var count: Int {
assert(keys.count == dict.count, "internal inconsistency")
return keys.count
@ole
ole / core-data-backup.swift
Last active January 1, 2024 16:52
How to make a copy of a Core Data SQLite database. See https://oleb.net/blog/2018/03/core-data-sqlite-backup/ for more.
import CoreData
import Foundation
/// Safely copies the specified `NSPersistentStore` to a temporary file.
/// Useful for backups.
///
/// - Parameter index: The index of the persistent store in the coordinator's
/// `persistentStores` array. Passing an index that doesn't exist will trap.
///
/// - Returns: The URL of the backup file, wrapped in a TemporaryFile instance
@yuvalt
yuvalt / UITextView+Placeholder.swift
Last active September 12, 2020 08:33 — forked from tijme/UITextViewPlaceholder.swift
The correct way to implement a placeholder in a UITextView (Swift). Allow the user to control the text, color, font and position of the placeholder. Everything (but font) can be modified from within Interface Builder.
//
// UITextViewPlaceholder.swift
// TextViewPlaceholder
//
// Original work Copyright (c) 2017 Tijme Gommers <tijme@finnwea.com>
// Modified work Copyright (c) 2017 Yuval Tal <yuvster@gmail.com>
//
// 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
@001101
001101 / accounting.sql
Created February 18, 2017 09:08 — forked from NYKevin/accounting.sql
Basic double-entry bookkeeping system, for PostgreSQL.
CREATE TABLE accounts(
id serial PRIMARY KEY,
name VARCHAR(256) NOT NULL
);
CREATE TABLE entries(
id serial PRIMARY KEY,
description VARCHAR(1024) NOT NULL,
amount NUMERIC(20, 2) NOT NULL CHECK (amount > 0.0),
-- Every entry is a credit to one account...
@mchirico
mchirico / knapsackFrack.py
Created January 11, 2017 16:03
knapsackFrack - for fractional weights
# knapsack.py
# A dynamic programming algorithm for the 0-1 knapsack problem and
# a greedy algorithm for the fractional knapsack problem
# Modified from: http://personal.denison.edu/~havill/algorithmics/python/knapsack.py
# A dynamic programming algorithm for the 0-1 knapsack problem.
def Knapsack01(v, w, W):
n = len(v) - 1
c = [] # create an empty 2D array c
@phatblat
phatblat / AppDelegate.swift
Last active May 4, 2024 12:34
Example of creating HKObserverQuery and enabling background delivery for multiple HKObjectType
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
let healthKitManager = HealthKitManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if onboardingComplete {
healthKitManager.requestAccessWithCompletion() { success, error in
if success { print("HealthKit access granted") }
else { print("Error requesting access to HealthKit: \(error)") }
}
@bcomnes
bcomnes / git-gpg.md
Last active February 13, 2024 07:33
my version of gpg on the mac
  1. brew install gnupg, pinentry-mac (this includes gpg-agent and pinentry)

  2. Generate a key: $ gpg --gen-key

  3. Take the defaults. Whatevs

  4. Tell gpg-agent to use pinentry-mac:

    $ vim ~/.gnupg/gpg-agent.conf 
    
@paragonie-scott
paragonie-scott / crypto-wrong-answers.md
Last active April 21, 2024 23:48
An Open Letter to Developers Everywhere (About Cryptography)
@dduan
dduan / UISegmentedControl+VerticalLayout.swift
Last active April 19, 2023 14:50
Turns a UISegmentedControl into a vertical layout.
import UIKit
extension UISegmentedControl {
func goVertical() {
self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
for segment in self.subviews {
for segmentSubview in segment.subviews {
if segmentSubview is UILabel {
(segmentSubview as UILabel).transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
@huonw
huonw / blackmagic.rs
Created January 15, 2014 12:42
do-while loops in Rust
while {
let x = foo();
bar(x);
x != 0
} {}