Skip to content

Instantly share code, notes, and snippets.

Avatar

Jeff Johnston hishma

View GitHub Profile
View UIFont.swift
import UIKit
extension UIFont {
/// Returns an instance of the font associated with the text style, specified design, and scaled appropriately for the user's selected content size category.
/// - Parameters:
/// - style: The text style for which to return a font. See UIFont.TextStyle for recognized values.
/// - weight: The weight of the font, specified as a font weight constant. For a list of possible values, see "Font Weights” in UIFontDescriptor. Avoid passing an arbitrary floating-point number for weight, because a font might not include a variant for every weight.
/// - fontDesign: The new system font design.
/// - Returns: A font object of the specified style, weight, and design.
@hishma
hishma / Button.swift
Created Apr 1, 2020
Hand rolled button, Heasley style.
View Button.swift
import UIKit
class Button: UIControl {
var labelText: String? {
set {
label.text = newValue
}
get {
label.text
}
View UIButton.swift
import UIKit
extension UIButton {
/// Sets the background color to use for the specified button state.
/// - Parameters:
/// - color: The background color to use for the specified state.
/// - forState: The state that uses the specified background color. The values are described in `UIControl.State`.
public func setBackgroundColor(_ color: UIColor, forState: UIControl.State) {
if let colorImage = self.imageWithColor(color) {
@hishma
hishma / String+RFC3986.swift
Created Oct 23, 2019
Percent encode a URL String
View String+RFC3986.swift
import Foundation
extension String {
public func addingPercentEncodingForRFC3986() -> String? {
let unreserved = "-._~/?"
let allowed = NSMutableCharacterSet.alphanumeric()
allowed.addCharacters(in: unreserved)
return self.addingPercentEncoding(withAllowedCharacters: allowed as CharacterSet)
}
@hishma
hishma / VariantHandlingErrorMiddleware.swift
Last active Sep 15, 2019
Vapor 3 middleware to respond with errors based on the requested media type
View VariantHandlingErrorMiddleware.swift
import Vapor
import LeafErrorMiddleware
import APIErrorMiddleware
/// Respond with errors based on the requested media type
public final class VariantHandlingErrorMiddleware: Middleware, ServiceType {
public static func makeService(for container: Container) throws -> Self {
return self.init(environment: container.environment)
}
@hishma
hishma / measure.swift
Created Sep 3, 2019
Prints the time taken to execute a closure.
View measure.swift
/// Prints the time taken to execute a closure.
///
/// Note: Only for debugging purposes.
public func measure<T>(_ label: String = "", _ f: () throws -> (T)) rethrows -> T {
let startTime = Date()
let result = try f()
let endTime = Date().timeIntervalSince(startTime)
print("\(label): Time taken", endTime)
return result
}
@hishma
hishma / open-hidden-files-on-macos.md
Last active Aug 30, 2019
Open hidden files on Mac OS
View open-hidden-files-on-macos.md

When the open panel is showing, press Command-Shift-. and the hidden files will appear.

View dateEncodingStrategy.md

TL;DR

If you are using the iso8601 strategy, you may not want to rely on the default Equatable implementation for Date (==) when comparing dates. You can use a good old fashion calendar comparison instead.

Strategies

JSONEncoder and JSONDecoder can both be configured with a strategy (dateEncodingStrategy and dateDecodingStrategy) for encoding and decoding dates. There are four named types in addition to allowing you to provide custom options.

| Strategy | Comment |

View XCTestCase+Error.swift
import XCTest
extension XCTestCase {
/// Asserts that an expression throws a specific error.
///
/// - Parameters:
/// - expression: An expression that can throw an error.
/// - throws: The expected `Error`. Must conform to the `Equatable` protocol.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which this function was called.
View CoreDataErrors.h
/*
CoreDataErrors.h
Core Data
Copyright (c) 2004-2012 Apple Inc.
All rights reserved.
*/
#import <Foundation/NSObject.h>
/* NSError codes for Core Data added errors in NSCocoaErrorDomain. Foundation error codes can be found in <Foundation/FoundationErrors.h>. AppKit error codes can be found in <AppKit/AppKitErrors.h>.
You can’t perform that action at this time.