Skip to content

Instantly share code, notes, and snippets.

@hishma
hishma / updateBuildNumber.sh
Last active April 13, 2023 15:12
Shell script to update the build number (CFBundleVersion) of the info.plist in the build folder.
# Shamelessly ripped of from https://blog.curtisherbert.com/automated-xcode-build-numbers-early-2019-edition/
#
git=`sh /etc/profile; which git`
bundleVersion=`"$git" rev-list --all | wc -l | tr -d '[:space:]'`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bundleVersion" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "☛ BUILD NUMBER: ${bundleVersion}"
@hishma
hishma / measure.swift
Last active April 5, 2019 12:37
Time a function in swift
@discardableResult
static func measure<T>(name: String = "", _ block: () -> T) -> T {
let startTime = CFAbsoluteTimeGetCurrent()
let result = block()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time: \(name) - \(timeElapsed)")
return result
}
/*
CoreDataErrors.h
Core Data
Copyright (c) 2004-2022, 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>.
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.

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 |

@hishma
hishma / open-hidden-files-on-macos.md
Last active August 30, 2019 15:17
Open hidden files on Mac OS

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

@hishma
hishma / measure.swift
Created September 3, 2019 17:17
Prints the time taken to execute a closure.
/// 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 / VariantHandlingErrorMiddleware.swift
Last active September 15, 2019 00:16
Vapor 3 middleware to respond with errors based on the requested media type
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 / String+RFC3986.swift
Created October 23, 2019 16:02
Percent encode a URL String
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)
}
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) {