Skip to content

Instantly share code, notes, and snippets.

@dagronf
dagronf / FileManager+extensionHidden.swift
Last active November 30, 2019 22:36
Swift extension for FileManager to show or hide a file's extension
/// Show or hide the extension of a file url
/// - Parameter ofItemAtFileURL: The file URL to change the extension visibility
/// - Parameter isHidden: The new state of the finder visibility. true == visible, false == hidden
func setExtensionVisibility(ofItemAtFileURL: URL, isHidden: Bool) throws {
try self.setAttributes([FileAttributeKey.extensionHidden : isHidden],
ofItemAtPath: ofItemAtFileURL.path)
}
@dagronf
dagronf / NSGraphicsContext+GStateManagement.swift
Last active November 27, 2019 01:57
Easier state management when working with NSGraphicsContext and CGContext (macOS/iOS/tvOS)
//
// NSGraphicsContext+GStateManagement.swift
//
// Created by Darren Ford on 10/11/19.
// Copyright © 2019 Darren Ford. All rights reserved.
//
// Easier state management when working with NSGraphicsContext and CGContext
//
#if os(macOS)
@dagronf
dagronf / Files+temporary.swift
Last active May 11, 2020 12:50
Temporary file and folder support for Files (https://github.com/JohnSundell/Files)
public extension Folder {
/// Create a new uniquely-named file within this folder.
/// - Parameter prefix: (optional) prefix to add the temporary file name
/// - Parameter fileExtension: (optional) file extension (without the `.`) to use for the created file
/// - Parameter contents: (optional) the data to write to the file
/// - throws: `WriteError` if a new file couldn't be created.
func createTemporaryFile(prefix: String? = nil, fileExtension: String? = nil, contents: Data? = nil) throws -> File {
var tempFilename = ""
if let prefix = prefix {
tempFilename += prefix + "_"
@dagronf
dagronf / DSFKeyedArchiverCodable.swift
Created November 18, 2019 01:22
A swift wrapper class to support Codable via NSKeyedArchiver
//
// DSFKeyedArchiverCodable.swift
// QR Generator
//
// Created by Darren Ford on 18/11/19.
// Copyright © 2019 Darren Ford. All rights reserved.
//
import Foundation
@dagronf
dagronf / FileManager+UniqueFileURL.swift
Last active November 27, 2019 01:50
An extension on FileManager in Swift that returns a unique file url given a 'base' filename and a destination folder url
extension FileManager {
/// Returns a unique URL for the specified filename and directory. Not thread safe
/// - Parameter filename: the `lastPathComponent` of a URL (for example, `IMG-2002.jpg`)
/// - Parameter directory: the directory in which to check
/// - Returns: a unique URL
static func UniqueFileURL(for filename: String, in directory: URL) -> URL {
// Filename is lastPathComponent
let fm = FileManager.default
@dagronf
dagronf / With.swift
Created October 7, 2019 23:42
SWIFT: Perform an immediate transform of a given subject
/// Perform an immediate `transform` of a given `subject`. The `transform`
/// function may just mutate the given `subject`, or replace it entirely.
///
/// ```
/// let oneAndTwo = with([1]) {
/// $0.append(2)
/// }
/// ```
///
/// - Parameters:
@dagronf
dagronf / Unique.swift
Last active October 4, 2019 23:24
SWIFT: Return the unique elements of a sequence, both using the Equatable protocol and by using a predicate
public extension Sequence {
/// Return unique elements in an array, given a predicate
/// - Parameter includeElement: block determining whether the elements are equivalent
func unique(_ predicate: (_ lhs: Element, _ rhs: Element) -> Bool) -> [Element] {
var results = [Element]()
forEach { (element) in
if results.filter( { predicate(element, $0) }).count == 0 {
results.append(element)
}
}
@dagronf
dagronf / XCTTest+extensions.swift
Created September 21, 2019 22:28
XCTTest helper function for unwrapping optionals within a test case using try/catch. Makes for a much cleaner test
/// From https://www.rightpoint.com/rplabs/xctassert-optional-unwrapping
// Usage :-
//
// class TestCaseUnwrap: XCTestCase {
// func testUnwrap() throws {
// let string: String? = nil
// let newString = try AssertNotNilAndUnwrap(string)
// XCTAssert(newString.lengthOfBytes(using: .utf8) > 0)
// }
@dagronf
dagronf / UIColor+contrasting.swift
Last active September 16, 2019 06:27
Get a contrasting UIColor for a background UIColor that could be used for text (for eg.)
import UIKit
extension UIColor {
private struct ColorComponents {
var r: CGFloat = 0.0
var g: CGFloat = 0.0
var b: CGFloat = 0.0
var a: CGFloat = 0.0
}
@dagronf
dagronf / UITextView+popover.swift
Created September 6, 2019 23:31
UITextView add other items to selection popover
let menuController = UIMenuController.shared
let item1 = UIMenuItem(title: "Item 1", action: #selector(performItem1))
let item2 = UIMenuItem(title: "Item 2", action: #selector(performItem2))
menuController.menuItems = [item1, item2]
menuController.setMenuVisible(true, animated: true)