Skip to content

Instantly share code, notes, and snippets.

View dave256's full-sized avatar

Dave Reed dave256

View GitHub Profile
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
default:
return nil
}
}
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? {
@dave256
dave256 / NotificationObserver.swift
Created January 12, 2018 00:10
block-based notifications
// from: https://twitter.com/jaredsinclair/status/951536021459619840
// typed in from the image, but not checked yet
class NotificationObserver {
private var observers: [NSObjectProtocol] = []
private let queue: OperationQueue
init(queue: OperationQueue = .main) {
self.queue = queue
}
import Foundation
extension Array {
/// move items at indices to index (insert them at index in the order of indices)
///
/// could be useful for dragging and dropping in a table view
///
/// - Parameters:
/// - indices: the indices to move
@dave256
dave256 / ImageTableViewCell.swift
Last active March 3, 2019 19:03
question for Cococasts video
//
// ImageTableViewCell.swift
// Images
//
// Created by Bart Jacobs on 31/07/2018.
// Copyright © 2018 Cocoacasts. All rights reserved.
//
import UIKit
//
// UIView+RSKeyboardLayoutGuide.swift
// RSTouchUIKit
//
// Created by Daniel Jalkut on 12/23/18.
//
import UIKit
// Extends UIView to expose a keyboardLayoutGuide property that can be used to tie a view controller's content
@dave256
dave256 / design.py
Last active September 2, 2020 19:23
design question
class EmailAddress:
def __init__(self, address: str, kind: str = "home"):
self.address = address
self.kind = kind
class Person:
def __init__(self, name: str, email: EmailAddress):
self.name = name
self.email = email
@dave256
dave256 / design.py
Last active September 2, 2020 19:23
design question
class Person:
def __init__(self, name: str, email: str):
self.name = name
self.email = email
def emailAddress(self):
return self.email
@dave256
dave256 / design.hpp
Created September 2, 2020 19:31
design question
#include <string>
class EmailAddress {
public:
EmailAddress(std::string name, std::string kind="home");
std::string address() const { return _email; }
std::string kind() const { return _kind; }
private:
std::string _email;
@dave256
dave256 / Decodable+URLSession.swift
Created November 26, 2020 21:15
Publisher for fetching Decodable from a URL
import Foundation
import Combine
struct APIError: Decodable, Error {
let statusCode: Int
}
// publisher code based on code in Practical Combine by Donny Wals
extension Decodable {
//
// StatefulPreviewWrapper.swift
// Do It
//
// Created by Jim Dovey on 10/11/19.
// Copyright © 2019 Jim Dovey. All rights reserved.
//
import SwiftUI
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)