FZSwiftUtils

1.1.1

Swift Foundation extensions, classes and utilities
flocked/FZSwiftUtils

What's New

1.1.1

2024-06-21T02:20:41Z

Updated

FZSwiftUtils

Swift Foundation extensions and useful classes & utilities.

For a full documentation take a look at the included documentation located at /Documentation. Opening the file launches Xcode's documentation browser.

Notable Extensions & Classes

DataSize

A data size abstraction.

var dataSize = DataSize(gigabytes: 1.5)
dataSize.megabyte // 1500 megabytes
dataSize.terabyte += 1
dataSize.string(includesUnit: true) // "1tb, 1gb, 500mb"
dataSize.string(for: .terabyte, includesUnit: false) // "1,15"

TimeDuration

A duration/time interval abstraction.

var duration = TimeDuration(seconds: 1)
duration.minutes += 2
duration.string(style: .full) // "2 minutes, 1 seconds"
duration.string(for: .seconds) =  "121 seconds"

// Duration between two dates.
let dateDuration = TimeDuration(from: date1, to: date2)

KeyValueObserver

Observes multiple properties of an object.

let textField = NSTextField()
let observer = KeyValueObserver(textField)
observer.add(\.stringValue) { oldStringValue, stringValue in
guard oldStringValue != stringValue else { return }
/// Process stringValue
}  

NSObject extensions

  • associatedValue: Getting and setting associated values of an object.
// Set
button.associatedValue["myAssociatedValue"] = "SomeValue"

// get
if let string: String = button.associatedValue["myAssociatedValue"] {

}
  • observeChanges<Value>(for: for keyPath: KeyPath<Self, Value>): Observes changes for a property.
textField.observeChanges(for \.stringValue) { oldStringValue, stringValue in
guard oldStringValue != stringValue else { return }
/// Process stringValue
}  

Progress extensions

  • updateEstimatedTimeRemaining(): Updates the estimted time remaining and throughput.
  • addFileProgress(url: URL, kind: FileOperationKind = .downloading): Shows the file progress in Finder.
progress.addFileProgress(url: fileURL, kind: .downloading)
  • MutableProgress: A progress that allows to add and remove children progresses.

Iterate directories & files

Addition URL methods for iterating the content of file system directories.

  • Iterate sub directories
for subDirectoryURL in downloadsDirectory.iterateDirectories() {
    
}
  • Iterate files
for fileURL in downloadsDirectory.iterateFiles() {
    
}
  • Iterate files by file extensions, file types or by predicate
// Iterates files with .txt extension
for txtFileURL in downloadsDirectory.iterateFiles(extensions: ["txt"]) {

}

// Iterate multimedia files
for multimediaFileURL in downloadsDirectory.iterateFiles(types: [.video, .image, .gif]) {

}

// Iterates video files with file names that contain "vid_" and finder tags containing "Favorite"
for fileURL in downloadsDirectory.iterate(.includeSubdirectoryDescendants, .includeHiddenFiles, predicate: { file in
    return file.fileType == .video &&
    file.lastPathComponent.contains("vid_") &&
    file.resources.finderTags.contains("Favorite")
}) {
    
}

You can also specifiy iterate options.

/// Iterates files, including files in subdirectories and hidden files
for fileURL in downloadsDirectory.iterateFiles(.includeSubdirectoryDescendants, .includeHiddenFiles) {
    
}

MeasureTime

Meassures the time executing a block.

let timeElapsed = MeasureTime.timeElapsed() {
/// The block to measure
}

OSHash

An implementation of the OpenSuptitle hash.

let hash = try? OSHash(url: fileURL)
hash?.Value /// The hash value

More…

  • AsyncOperation: An asynchronous, pausable operation.
  • PausableOperationQueue: A pausable operation queue.
  • SynchronizedArray/SynchronizedDictionary: A synchronized array/dictioanry.
  • Equatable.isEqual(_ other: any Equatable) -> Bool: Returns a Boolean value indicating whether the value is equatable to another value.
  • Comparable.isLessThan(_ other: any Comparable) -> Bool: Returns a Boolean value indicating whether the value is less than another value.

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Dec 21 2024 07:43:38 GMT-1000 (Hawaii-Aleutian Standard Time)