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
/// A simple layer class to expose an animation progress for a CAAnimation. | |
public class ArbitraryAnimationLayer: CALayer { | |
static let KeyPath: String = "progress" | |
override init() { | |
super.init() | |
} | |
var progressCallback: ((CGFloat) -> Void)? |
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
/// Provide the equivalent of @synchronised on objc | |
private func synchronized<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T { | |
objc_sync_enter(lock) | |
defer { objc_sync_exit(lock) } | |
return try body() | |
} | |
/// Simple lockable class | |
class Lockable { | |
private var lockable: AnyObject |
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 Cocoa | |
class AutoselectOnFocusTextField: NSTextField { | |
override func becomeFirstResponder() -> Bool { | |
guard super.becomeFirstResponder() else { | |
return false | |
} | |
if let editor = self.currentEditor() { | |
editor.perform(#selector(selectAll(_:)), with: self, afterDelay: 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
// Applescript: tell application "System Preferences" to get anchors of current pane | |
// Result: | |
// { anchor "Privacy_Reminders" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_SystemServices" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Calendars" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Firewall" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Assistive" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_LinkedIn" of pane id "com.apple.preference.security" of application "System Preferences", | |
// anchor "Privacy_Accessibility" of pane id "com.apple.preference.security" of application "System Preferences", |
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) |
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
/// 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
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
/// 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
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 |