{"id":19190837,"url":"https://github.com/flocked/fzswiftutils","last_synced_at":"2025-05-08T04:49:30.700Z","repository":{"id":169211960,"uuid":"645111990","full_name":"flocked/FZSwiftUtils","owner":"flocked","description":"Swift Foundation extensions, classes and utilities","archived":false,"fork":false,"pushed_at":"2025-05-05T02:27:02.000Z","size":33526,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-08T04:49:17.455Z","etag":null,"topics":["foundation","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flocked.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-05-25T00:32:30.000Z","updated_at":"2025-05-05T02:27:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"30869a06-d84a-40fa-9de1-b5249d4ecf93","html_url":"https://github.com/flocked/FZSwiftUtils","commit_stats":null,"previous_names":["flocked/fzswiftutils"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flocked%2FFZSwiftUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flocked%2FFZSwiftUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flocked%2FFZSwiftUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flocked%2FFZSwiftUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flocked","download_url":"https://codeload.github.com/flocked/FZSwiftUtils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253002842,"owners_count":21838637,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["foundation","swift"],"created_at":"2024-11-09T11:35:57.401Z","updated_at":"2025-05-08T04:49:30.612Z","avatar_url":"https://github.com/flocked.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FZSwiftUtils\n\nSwift Foundation extensions and useful classes \u0026 utilities.\n\n**For a full documentation take a look at the included documentation located at */Documentation*. Opening the file launches Xcode's documentation browser.**\n\n## Notable Extensions \u0026 Classes\n\n### DataSize\n\nA data size abstraction.\n\n```swift\nvar dataSize = DataSize(gigabytes: 1.5)\ndataSize.megabyte // 1500 megabytes\ndataSize.terabyte += 1\ndataSize.string(includesUnit: true) // \"1tb, 1gb, 500mb\"\ndataSize.string(for: .terabyte, includesUnit: false) // \"1,15\"\n```\n\n### TimeDuration\n\nA duration/time interval abstraction.\n\n```swift\nvar duration = TimeDuration(seconds: 1)\nduration.minutes += 2\nduration.string(style: .full) // \"2 minutes, 1 seconds\"\nduration.string(for: .seconds) =  \"121 seconds\"\n\n// Duration between two dates.\nlet dateDuration = TimeDuration(from: date1, to: date2)\n```\n\n### NSObject property observation\n\n- Observe KVO properties of a `NSObject` using `observeChanges(for:)`. \n\nIt returns `KeyValueObservation` that you need to save as long as you want to observe the property.\n\n```swift\nlet textField = NSTextField()\n\nlet stringObservation = textField.observeChanges(for \\.stringValue) { oldStringValue, stringValue in\n    /// stringValue changed\n}\n```\n\n- Observe multiple properties using `KeyValueObserver`:\n\n```swift\nlet textFieldObserver = KeyValueObserver(textField)\ntextFieldObserver.add(\\.stringValue) { oldStringValue, stringValue in\n    /// stringValue changed\n}\ntextFieldObserver.add(\\.font) { oldFont, font in\n    /// font changed\n}\ntextFieldObserver.add(\\.textColor) { oldTextColor, textColor in\n    /// textColor changed\n}\n```\n\n### NSObject Associated Values\n\nAssociated Values allows you to add additional properties to a `NSObject`:\n\n```swift\n// Set\nview.associatedValue[\"backgroundColor\"] = NSColor.black\n\n// get\nif let backgroundColor: NSColor = view.associatedValue[\"backgroundColor\"] {\n\n}\n\n// Or easily extend objects:\nextension NSView {\n    var backgroundColor: NSColor? {\n        get { associatedValue[\"backgroundColor\"] }\n        set { \n            associatedValue[\"backgroundColor\"] = newValue\n            …\n        }\n    }\n}\n```\n\n### Iterate directories \u0026 files\n\nAddition `URL` methods for iterating the content of file system directories.\n \n  - Iterate sub folders:\n \n ```swift\n for folderURL in downloadsDirectory.iterateFolders() {\n     \n }\n ```\n \n  - Iterate files:\n \n ```swift\n for fileURL in downloadsDirectory.iterateFiles() {\n     \n }\n ```\n \n - Iterate files recursively (including the files of sub folders) and include hidden files:\n \n  ```swift\n for fileURL in downloadsDirectory.iterateFiles().recursive.includingHidden {\n     \n }\n ```\n \n - Iterate files by file extensions, file types or by predicate:\n \n ```swift\n // Iterate multimedia files\n for multimediaFileURL in downloadsDirectory.iterateFiles(types: [.video, .image, .gif]) {\n \n }\n \n  // Iterates files with .txt extension\n for txtFileURL in downloadsDirectory.iterateFiles(extensions: [\"txt\"]) {\n\n }\n \n // Iterates video files with file names that contain \"vid_\"\n for fileURL in downloadsDirectory.iterate(predicate: { file in\n     return file.fileType == .video \u0026\u0026 file.lastPathComponent.contains(\"vid_\")\n }).recursive.includingHidden {\n     \n }\n ```\n \n ### File System URL Resources\n\nGet properties of a file system resource (like creation date, file size or finder tags) using `URL.resources`:\n\n```swift\nlet creationDate = fileURL.resources.creationDate\nlet fileSize = fileURL.resources.fileSize\nlet finderTags = fileURL.resources.finderTags\n```\n\n### MeasureTime\n\nMeassures the time executing a block.\n\n```swift\nMeasureTime.printTimeElapsed() {\n/// The block to measure\n}\n```\n\n### NSObject Class Reflection\n\nReflects all properties, methods and ivars of a NSObject class including hidden ones.\n\n```swift\n/// All properties, methods and ivars of `NSView`:\nSwift.print(NSView.classReflection())\n\n/// All class properties of `UIView`:\nSwift.print(UIView.classReflection().classProperties)\n```\n\n### OSHash\n\nAn implementation of the OpenSuptitle hash.\n\n```swift\n\nlet hash = try? OSHash.hash(url: fileURL)\nhash?.hashValue /// The hash value\n```\n\n### Progress extensions\n\n- `autoUpdateEstimatedTimeRemaining`: Updates the estimted time remaining and throughput.\n- `addFileProgress(url: URL)`: Shows the file progress in Finder.\n\n```swift\nprogress.addFileProgress(url: fileURL, kind: .downloading)\n```\n\n- `MutableProgress`: A progress that allows to add and remove children progresses.\n\n### Notification Observation Using Blocks\n\nObserve `NotificationCenter` notifications using a block.\n\nUse NotificationCenter`s `observe(name:object:block:)`. It returns `NotificationToken` that you need to save as long as you want to observe the notification.\n\n\n```swift\nlet viewFrameNotificationToken = NotificationCenter.default.observe(NSView.frameDidChangeNotification, object: view) { _ in \n}\n```\n\n### More…\n\n- `AsyncOperation`: An asynchronous, pausable operation.\n- `PausableOperationQueue`: A pausable operation queue.\n- `SynchronizedArray`/`SynchronizedDictionary`: A synchronized array/dictionary.\n- `Equatable.isEqual(_ other: any Equatable) -\u003e Bool`: Returns a Boolean value indicating whether the value is equatable to another value.\n- `Comparable.isLessThan(_ other: any Comparable) -\u003e Bool`: Returns a Boolean value indicating whether the value is less than another value.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflocked%2Ffzswiftutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflocked%2Ffzswiftutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflocked%2Ffzswiftutils/lists"}