Skip to content

Instantly share code, notes, and snippets.

Avatar

Sindre Sorhus sindresorhus

View GitHub Profile
@sindresorhus
sindresorhus / SwiftUI Placeholder idea.swift
Last active Aug 22, 2022
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?
View SwiftUI Placeholder idea.swift
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)
}
View Collection+safe.swift
extension Collection {
/**
Returns the element at the given index if any, otherwise `nil`.
```
guard let element = array[position] else {
return
}
print(element)
View NSView+shake.swift
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
View Sequence+splittingEvenly.swift
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? {
View UnexpectedNilError.swift
// 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 Nov 3, 2021
How to use `PHImageManager#requestImage` with async/await in Swift.
View PHImageManager-requestImage-async.swift
import Photos
struct UnexpectedNilError: Error {}
extension PHImageManager {
func requestImage(
for asset: PHAsset,
targetSize: CGSize,
contentMode: PHImageContentMode,
options: PHImageRequestOptions?
View swiftui-shape-extensions.swift
/**
Before:
```
.clipShape(Rectangle())
```
After:
```
View UIImage+asActivityItemSource.swift
extension UIImage {
private final class UIImageActivityItemSource: NSObject, UIActivityItemSource {
var image = UIImage()
var title: String?
var url: URL?
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
UIImage()
}
@sindresorhus
sindresorhus / machine-learning-for-my-apps.md
Last active Jul 14, 2022
Ideas for machine learning usage in my apps
View machine-learning-for-my-apps.md

Machine learning for my apps

I'm trying to think of ways I could use machine learning to enhance my apps. Feedback wanted!

You can find my apps here.

Dato

  • Natural language processing to parse a new event title, like Fantastical.
@sindresorhus
sindresorhus / IdentifiableByHashable.swift
Created Mar 10, 2021
Make a type `Identifiable` based on its `Hashable` hash value.
View IdentifiableByHashable.swift
/**
Make a type `Identifiable` based on its `Hashable` hash value.
This can be useful when all the properties are required to represent its identifier.
```
struct Item: Hashable, IdentifiableByHashable {
let title: String
var message: String?
}