This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "_" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DSFKeyedArchiverCodable.swift | |
// QR Generator | |
// | |
// Created by Darren Ford on 18/11/19. | |
// Copyright © 2019 Darren Ford. All rights reserved. | |
// | |
import Foundation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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) | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |