Skip to content

Instantly share code, notes, and snippets.

@dagronf
dagronf / DataToStringReversableTransformer.swift
Created July 20, 2020 02:33
Swift-based Data to String reversible ValueTransformer supporting different string encodings (allowing binding from NSData to NSTextField and back for example)
@objc class DataToStringReversableTransformer: ValueTransformer {
static func RegisterTransformer() {
ValueTransformer.setValueTransformer(
DataToStringReversableTransformer(encoding: .utf8),
forName: NSValueTransformerName("DataToStringReversableTransformer_utf8")
)
ValueTransformer.setValueTransformer(
DataToStringReversableTransformer(encoding: .utf16),
forName: NSValueTransformerName("DataToStringReversableTransformer_utf16")
)
@dagronf
dagronf / Clamp.swift
Created July 5, 2020 20:07
Swift extensions for floating point and integer range clamping (swift, clamp)
//
// Clamp.swift
//
// Created by Darren Ford on 6/7/20.
// Copyright © 2020 Darren Ford. All rights reserved.
//
import Foundation
public extension ExpressibleByIntegerLiteral where Self: FloatingPoint {
@dagronf
dagronf / MarchingAntsSelectionLayer.swift
Created July 4, 2020 02:11
A simple marching ants CALayer implementation
class MarchingAntsSelectionLayer: CAShapeLayer {
func setup() {
let dashPattern: [NSNumber] = [2.0, 2.0]
let dashPhase = dashPattern.map { $0.floatValue }.reduce(0) { $0 + $1 }
self.lineDashPattern = dashPattern
let lineDashPhaseAnimation = CABasicAnimation(keyPath: "lineDashPhase")
lineDashPhaseAnimation.byValue = dashPhase
lineDashPhaseAnimation.duration = 0.5
@dagronf
dagronf / NSImage+copyAsRGBA.swift
Last active June 29, 2020 05:05
An NSImage extension that converts an NSImage in ANY supported format to a new NSImage using the generic RGB colorspace.
extension NSImage {
/// An NSImage extension that converts an NSImage in ANY format to a new NSImage using the calibrated RGB colorspace.
///
/// Useful when you have to support a 3rd party tool that has format limitations, such as not supporting
/// paletted PNG files or grayscale colorspaces (I'm looking at you Create ML!)
func copyAsCalibratedRGBColorspace() -> NSImage? {
guard let rep = self.representations.first else {
return nil
}
@dagronf
dagronf / String+LazyIndexExtensions.swift
Created April 6, 2020 06:05
A set of lazy(!) extensions for String to allow index-based indexing
//
// String+extensions.swift
//
// Created by Darren Ford on 6/4/20.
//
// Lazy extensions for Swift Strings indexing
//
// For example :-
// let value = "Caterpillar"
// let catStr = value.substring(0 ..< 3)
@dagronf
dagronf / AppKit+propertyStreamer.swift
Created March 2, 2020 22:36
SWIFT: Stream generic functions for NSView and NSCell to allow quick initialisation of properties
/// Stream generic functions for NSView and NSCell to allow quick initialisation of properties
/// Useful for when your (eg) button variable has a long name or is optional.
func <<<T> (left: T, configureBlock: (T) -> Void) -> T where T: NSView {
configureBlock(left)
return left
}
func <<<T> (left: T, configureBlock: (T) -> Void) -> T where T: NSCell {
configureBlock(left)
return left
@dagronf
dagronf / NSControl.StateValue+named.swift
Created February 17, 2020 06:36
NSControl.StateValue extension for CustomStringConvertible
extension NSControl.StateValue: CustomStringConvertible {
public var description: String {
switch self {
case .on: return NSLocalizedString("On", comment: "State for when a control is 'on'")
case .mixed: return NSLocalizedString("Mixed", comment: "State for when a control is neither on of off")
case .off: return NSLocalizedString("Off", comment: "State for when a control is 'off'")
default: fatalError("unimplemented state")
}
}
}
@dagronf
dagronf / NSColor+contrastingTextColor.swift
Created February 17, 2020 05:42
Returns a contrasting NSColor for this NSColor. Useful for putting text on a plain color background or the like...
extension NSColor {
private struct ColorComponents {
var r: CGFloat = 0.0
var g: CGFloat = 0.0
var b: CGFloat = 0.0
var a: CGFloat = 0.0
}
private func components() -> ColorComponents {
var result = ColorComponents()
@dagronf
dagronf / String+BoolInterpreter.swift
Created January 7, 2020 00:35
Swift String extension to use NSString's 'bool' interpretation rules.
import Foundation
extension String {
/// Retrieve the contents of the string as a Bool value.
///
/// This property is true on encountering one of "Y", "y", "T", "t", or a digit 1-9—the method ignores any trailing characters. This property is false if the receiver doesn’t begin with a valid decimal text representation of a number.
///
/// The property assumes a decimal representation and skips whitespace at the beginning of the string. It also skips initial whitespace characters, or optional -/+ sign followed by zeroes.
///
/// See: [NSString boolValue](https://developer.apple.com/documentation/foundation/nsstring/1409420-boolvalue)
@dagronf
dagronf / RandomArray.swift
Created December 27, 2019 08:50
Generate an array of random values for Swift value types that support random value generation
/// Generate an array of random values for Swift value types that support random value generation
/// - Parameters:
/// - count: The number of random numbers to generate
/// - range: The range of values to generate
///
/// Example:
///
/// ```swift
/// let tenVals: [CGFloat] = RandomArray(count: 10, range: -10 ... 10)
/// ```