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 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) | |
} | |
} |
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
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) | |
} | |
} |
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
private protocol SilenceDeprecationForUIScreenWindows { | |
var screens: [UIScreen] { get } | |
} | |
private final class SilenceDeprecationForUIScreenWindowsImplementation: SilenceDeprecationForUIScreenWindows { | |
@available(iOS, deprecated: 16) | |
var screens: [UIScreen] { UIScreen.screens } | |
} | |
extension UIScreen { |
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
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) | |
} |
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 Collection { | |
/** | |
Returns the element at the given index if any, otherwise `nil`. | |
``` | |
guard let element = array[position] else { | |
return | |
} | |
print(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
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 |
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 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? { |
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
// 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 |
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 Photos | |
struct UnexpectedNilError: Error {} | |
extension PHImageManager { | |
func requestImage( | |
for asset: PHAsset, | |
targetSize: CGSize, | |
contentMode: PHImageContentMode, | |
options: PHImageRequestOptions? |
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
/** | |
Before: | |
``` | |
.clipShape(Rectangle()) | |
``` | |
After: | |
``` |
NewerOlder