Skip to content

Instantly share code, notes, and snippets.

View orchetect's full-sized avatar
💻
Building apps and taking naps

Steffan Andrews orchetect

💻
Building apps and taking naps
  • Steffan Andrews Music Inc.
  • Vancouver, BC, Canada
  • 17:51 (UTC -07:00)
View GitHub Profile
@orchetect
orchetect / SwiftUI MenuBarExtra Toggle.md
Last active February 27, 2023 12:18
Programatically toggle SwiftUI MenuBarExtra menu/window presentation state.
@orchetect
orchetect / Weak.swift
Last active September 23, 2022 04:30
Weak.swift
//
// Weak.swift
//
// Created by Steffan Andrews on 2019-11-16.
// Copyright © 2019 Steffan Andrews. All rights reserved.
//
// MARK: - Weak<T>
/// Wrapper that contains a weak reference to an object.
extension Result {
@_disfavoredOverload
public mutating func set(_ newSuccess: Success) {
self = .success(newSuccess)
}
@_disfavoredOverload
public mutating func set(_ newFailure: Failure) {
self = .failure(newFailure)
}
import AppKit
// MARK: - App Icons
func getIcon(file path: String) -> NSImage? {
guard FileManager.default.fileExists(atPath: path)
else { return nil }
return NSWorkspace.shared.icon(forFile: path)
}
infix operator ?|: NilCoalescingPrecedence
extension Collection {
/** Elvis Operator (`?:` or `||` in other languages)
Works on String, Array, Collection, Set, etc.
Returns the left-hand value if it is not empty.
"" ?| "Default" // "Default"
"String" ?| "Default" // "String"
#if os(macOS)
import Foundation
/// On init, parses Dropbox's info.json file and populates the pertinent data into variables. Object is nil if info.json file can't be found (which usually means Dropbox is not installed on the system) or there was a generic error parsing the JSON data.
///
/// Properties are populated once upon the struct's init. There is no method to refresh the data. Instead, instance a new copy of `DropboxPaths`.
///
/// - `personalPath`: POSIX path if found; nil if not discoverable
/// - `personalURL`: URL if found; nil if not discoverable
import Foundation
/// Returns an array of random numbers. Values will be beween the range given, with an array size of `count`.
///
/// Example:
/// ```
/// [UInt8](randomValuesBetween: 0...255, count: 4) // [4,75,241,176]
/// ```
extension Array where Element : FixedWidthInteger {
@orchetect
orchetect / Semaphored propertyWrapper.swift
Last active October 20, 2021 02:42
Swift 5 propertyWrapper that wraps a value with a semaphore to prevent overlapping access.
/// Wraps a value with a DispatchSemaphore(value: 1) to prevent overlapping access.
@propertyWrapper public struct Semaphored<T> {
private var value: T
private var semaphore = DispatchSemaphore(value: 1)
public var wrappedValue: T {
get {
defer { semaphore.signal() }
semaphore.wait()