xcrun simctl --set previews delete all
xcrun simctl delete unavailable
| func customLog(_ category: String) -> OSLog { | |
| #if DEBUG | |
| return OSLog(subsystem: Bundle.main.bundleIdentifier!, category: category) | |
| #else | |
| return OSLog.disabled | |
| #endif | |
| } |
| // Zalgo text: https://en.wikipedia.org/wiki/Zalgo_text | |
| func zalgo(_ string: String, intensity: Int = 5) -> String { | |
| let combiningDiacriticMarks = 0x0300...0x036f | |
| let latinAlphabetUppercase = 0x0041...0x005a | |
| let latinAlphabetLowercase = 0x0061...0x007a | |
| var output: [UnicodeScalar] = [] | |
| for scalar in string.unicodeScalars { | |
| output.append(scalar) | |
| guard (latinAlphabetUppercase).contains(numericCast(scalar.value)) || |
| extension ProcessInfo { | |
| var isSwiftUIPreview: Bool { | |
| environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" | |
| } | |
| // or: | |
| static func isOnPreview() -> Bool { | |
| return processInfo.processName == "XCPreviewAgent" |
| // Credit: https://www.apeth.com/UnderstandingCombine/subscribers/subscribersoneshot.html | |
| var cancellable: AnyCancellable? | |
| cancellable = pub.sink(receiveCompletion: {_ in | |
| cancellable?.cancel() | |
| }) { image in | |
| self.imageView.image = image | |
| } | |
| // Or just subscribe to the Subscribers.Sink: |
| import SwiftUI | |
| public struct ScrollToModifier<T: Hashable>: ViewModifier { | |
| @Binding var id: T? | |
| public func body(content: Content) -> some View { | |
| ScrollViewReader { proxy in | |
| content | |
| .onChange(of: id) { newValue in | |
| if let id { | |
| scrollTo(id: id, using: proxy) |
| // Code from: https://alexdremov.me/swift-actors-common-problems-and-tips/ | |
| import Foundation | |
| actor ActivitiesStorage { | |
| var cache = [UUID: Task<Data?, Never>]() | |
| func retrieveHeavyData(for id: UUID) async -> Data? { | |
| if let task = cache[id] { | |
| return await task.value |
| extension View { | |
| func onTapGesture(perform action: () async -> Void) -> some View { | |
| self.onTapGesture { | |
| Task { await action() } | |
| } | |
| } | |
| } |
| import Foundation | |
| enum State { | |
| case idle | |
| case running(speed: Double) | |
| case paused(duration: TimeInterval) | |
| case stopped(reason: String) | |
| } | |
| let states: [State] = [.idle, .paused(duration: 2), .running(speed: 10), .running(speed: 2)] |
| private func fileComponents(_ fileName: String) -> (name: String, extension: String) { | |
| let fileExtension = URL(fileURLWithPath: fileName).pathExtension | |
| let fileNameOnly = fileExtension.isEmpty ? fileName : String(fileName.dropLast(fileExtension.count + 1)) | |
| return (fileNameOnly, fileExtension) | |
| } |