Back to Groups
Development

Swift: macOS files and application state

Swift 5.9+ on macOS 14+: persisted app state, security-scoped files, bookmarks, cancellable debounce and Foundation parsing. Review names and integration points before use.

workflowsswift-developerfoundationcodeswift
OL
ollieb
0likes12snippets

Try it here

Loading snippet…

Your typing stays in this tab. Nothing is saved or sent.
Snippets (12)
;sw-httpjson
Decode JSON only after checking HTTP status
import Foundation enum HTTPPayloadError: Error { case nonHTTPResponse case status(Int) } f
;sw-query
Add URL query values without string concatenation
import Foundation func appendingQueryItems( _ items: [URLQueryItem], to url: URL ) throws -> UR
;sw-jsonfile
Encode JSON before replacing a local file
import Foundation func writeJSONAtomically<Value: Encodable>( _ value: Value, to destination: U
;sw-utc-stamp
Parse internet timestamps with optional fractional seconds
import Foundation func parseInternetTimestamp(_ text: String) -> Date? { let formatter = ISO860
;sw-batches
Split an array into bounded batches
enum BatchSizeError: Error { case nonPositive } func batches<Element>( of values: [Element], si
;sw-unique
Remove duplicate values while keeping first occurrence order
func uniqueInOrder<Element: Hashable>( _ values: [Element] ) -> [Element] { var seen = Set<E
;sw-capture
Read a regex capture using safe String indices
import Foundation func firstCapture( in text: String, pattern: String, group: Int = 1 ) throws
;sw-readfile
Read a local file with an explicit size limit
import Foundation enum BoundedFileError: Error { case invalidLimit, tooLarge } func readBoundedFil
;sw-statestore
Serialize app-state edits and persist before committing in memory
import Foundation actor JSONStateStore<Value: Codable & Sendable> { private var value: Value
;sw-bookmark
Resolve a saved file bookmark and report whether it is stale
import Foundation struct ResolvedFileBookmark { let url: URL let needsRefresh: Bool } func
;sw-scopedaccess
Balance security-scoped access around a file operation
import Foundation func withSecurityScopedAccess<Result>( to url: URL, operation: (URL) throws -
;sw-debounce
Debounce a search or autosave task with explicit cancellation
actor TaskDebouncer { private var pending: Task<Void, Never>? @discardableResult func s