Skip to content

Instantly share code, notes, and snippets.

@bill1m
bill1m / WeekdaySet.swift
Last active September 19, 2023 13:12
OptionSet CaseIterable enum
/// Day rawValues used in WeekdaySet. Day proper names capitalized.
enum Day: UInt8, CaseIterable {
case Sunday = 0b00000001
case Monday = 0b00000010
case Tuesday = 0b00000100
case Wednesday = 0b00001000
case Thursday = 0b00010000
case Friday = 0b00100000
case Saturday = 0b01000000
var description: String {
@bill1m
bill1m / StdLibExtensions.swift
Last active October 12, 2020 16:54
UInt64 and UInt16 extensions to mask the bits and convert to String
//
// StdLibExtensions.swift
//
// Created by Bill Morrison on 1/19/19.
//
import Foundation
extension UInt64 {
@bill1m
bill1m / duration.swift
Last active October 12, 2020 16:49
Swift function to measure time to execute a block of code.
/*
* Copyright (c) 2016 Razeware LLC
*
* 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:
*
@bill1m
bill1m / URLSession_DataTaskPublisher.swift
Last active March 31, 2020 16:34
Processing URL Session Data Task Results with Combine
// https://developer.apple.com/documentation/foundation/urlsession/processing_url_session_data_task_results_with_combine
import Foundation
import Combine
let cancellable = URLSession.shared
.dataTaskPublisher(for: URL(string: "https://jsonplaceholder.typicode.com/users")!)
.tryMap() { element -> Data in
guard let httpResponse = element.response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
//: These Swift examples follow the Objective-C found in [NSRegularExpression](https://developer.apple.com/documentation/foundation/nsregularexpression)
import Foundation
let regex1 = try! NSRegularExpression(pattern: "\\b(a|b)(c|d)\\b", options: [.caseInsensitive])
let string1 = "Test with Ad and ad and Ac and AC and bD and bd and Bc and BC in the string."
let range1 = NSMakeRange(0, string1.count)
@bill1m
bill1m / BeersOnWall.swift
Last active February 26, 2020 17:27
Xcode playground localizedStringWithFormat
import Cocoa
import PlaygroundSupport
//: ### Put Localizable.stringsdict in ~/Documents/Shared Playground Data
let bundle = Bundle(url: playgroundSharedDataDirectory)!
let format1 = NSLocalizedString("beers_on_wall", tableName: nil, bundle: bundle, value: "", comment: "")
@bill1m
bill1m / ComplexClosureReturnType.swift
Last active February 27, 2020 13:30
Unable to infer complex closure return type; add explicit type to disambiguate
// DestinationsView.swift
// PdfDocumentBrowser
//
// Created by Bill Morrison on 2/22/20.
// Copyright © 2020 Bill Morrison. All rights reserved.
//
import SwiftUI
import PDFKit