Skip to content

Instantly share code, notes, and snippets.

View sindresorhus's full-sized avatar
🌴
On vacation

Sindre Sorhus sindresorhus

🌴
On vacation
View GitHub Profile
extension CaseIterable {
public static func randomCaseIterableElement(using generator: inout some RandomNumberGenerator) -> Self {
allCases.randomElement(using: &generator)!
}
public static func randomCaseIterableElement() -> Self {
var generator = SystemRandomNumberGenerator()
return randomCaseIterableElement(using: &generator)
}
}
@sindresorhus
sindresorhus / NSSharingService-sharingServices-deprecation-warning-fix.swift
Created October 19, 2022 07:56
macOS 13 deprecated `NSSharingService.sharingServices()`. Here's how to silence the deprecation until you can move to the new API.
private protocol SilenceDeprecationNSSharingService {
func sharingServices(forItems items: [Any]) -> [NSSharingService]
}
private final class SilenceDeprecationNSSharingServiceImplementation: SilenceDeprecationNSSharingService {
@available(macOS, deprecated: 13)
func sharingServices(forItems items: [Any]) -> [NSSharingService] {
NSSharingService.sharingServices(forItems: items)
}
}
@sindresorhus
sindresorhus / UIScreen-screens-deprecation-warning-fix.swift
Last active October 19, 2022 21:46
iOS 16 deprecated `UIScreen#screens`. Here's how to silence the deprecation until you can move to the new API.
private protocol SilenceDeprecationForUIScreenWindows {
var screens: [UIScreen] { get }
}
private final class SilenceDeprecationForUIScreenWindowsImplementation: SilenceDeprecationForUIScreenWindows {
@available(iOS, deprecated: 16)
var screens: [UIScreen] { UIScreen.screens }
}
extension UIScreen {
@sindresorhus
sindresorhus / SwiftUI Placeholder idea.swift
Last active August 22, 2022 02:59
For a macOS app, if you want an action in both the main menu and the UI, it's quite boilerplaty. You need to synchronize some value with @focusedvalue. What if we had something like this?
struct CameraCommands: Commands {
var body: some Commands {
CommandGroup(replacing: .newItem) {
// This menu item will be enabled when the key view has a button with `.command(.takePhoto)`. The action logic is implemented in the button.
Placeholder("Take Photo", id: .takePhoto)
// Alternatively, it could be:
// Button("Take Photo") {}
// .placeholder(.takePhoto)
}
extension Collection {
/**
Returns the element at the given index if any, otherwise `nil`.
```
guard let element = array[position] else {
return
}
print(element)
@sindresorhus
sindresorhus / NSView+shake.swift
Created January 12, 2022 17:25
Shake a NSView
extension NSView {
/**
Shake the view.
- Note: It will do nothing if the user has enabled the “Reduce motion” accessibility preference.
*/
func shake(
duration: TimeInterval = 0.3,
direction: NSUserInterfaceLayoutOrientation,
moveAmount: Double = 5
extension Collection where Element: Comparable {
/**
Returns the index of the first occurrence of the lowest value in the collection.
```
[4, 2, 3, 2, 5].firstIndexOfMinElement()
//=> 1
```
*/
func firstIndexOfMinElement() -> Index? {
// Using `LocalizedError` here as the `.localizedDescription` for `Error` is often missing in places like an alert and Crashlytics/Sentry.
struct UnexpectedNilError: LocalizedError {
let message: String?
let file: String
let line: Int
init(
_ message: String?,
file: String = #fileID,
line: Int = #line
@sindresorhus
sindresorhus / PHImageManager-requestImage-async.swift
Created November 3, 2021 05:46
How to use `PHImageManager#requestImage` with async/await in Swift.
import Photos
struct UnexpectedNilError: Error {}
extension PHImageManager {
func requestImage(
for asset: PHAsset,
targetSize: CGSize,
contentMode: PHImageContentMode,
options: PHImageRequestOptions?
@sindresorhus
sindresorhus / swiftui-shape-extensions.swift
Created November 2, 2021 05:53
Nicer `Shape` access in SwiftUI
/**
Before:
```
.clipShape(Rectangle())
```
After:
```