FZSwiftUtils

Swift Foundation 的扩展以及有用的类和实用工具。

要获得完整的文档,请查看位于/Documentation中的包含的文档。打开该文件会启动 Xcode 的文档浏览器。

值得注意的扩展和类

DataSize

数据大小的抽象表示。

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

持续时间/时间间隔的抽象表示。

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)

NSObject 属性观察

它返回 KeyValueObservation,只要你想观察该属性,就需要保存它。

let textField = NSTextField()

let stringObservation = textField.observeChanges(for \.stringValue) { oldStringValue, stringValue in
    /// stringValue changed
}
let textFieldObserver = KeyValueObserver(textField)
textFieldObserver.add(\.stringValue) { oldStringValue, stringValue in
    /// stringValue changed
}
textFieldObserver.add(\.font) { oldFont, font in
    /// font changed
}
textFieldObserver.add(\.textColor) { oldTextColor, textColor in
    /// textColor changed
}

NSObject 关联值

关联值允许你向 NSObject 添加额外的属性

// Set
view.associatedValue["backgroundColor"] = NSColor.black

// get
if let backgroundColor: NSColor = view.associatedValue["backgroundColor"] {

}

// Or easily extend objects:
extension NSView {
    var backgroundColor: NSColor? {
        get { associatedValue["backgroundColor"] }
        set { 
            associatedValue["backgroundColor"] = newValue
            
        }
    }
}

迭代目录和文件

用于迭代文件系统目录内容的附加 URL 方法。

for folderURL in downloadsDirectory.iterateFolders() {
    
}
for fileURL in downloadsDirectory.iterateFiles() {
    
}
for fileURL in downloadsDirectory.iterateFiles().recursive.includingHidden {
   
}
// Iterate multimedia files
for multimediaFileURL in downloadsDirectory.iterateFiles(types: [.video, .image, .gif]) {

}

 // Iterates files with .txt extension
for txtFileURL in downloadsDirectory.iterateFiles(extensions: ["txt"]) {

}

// Iterates video files with file names that contain "vid_"
for fileURL in downloadsDirectory.iterate(predicate: { file in
    return file.fileType == .video && file.lastPathComponent.contains("vid_")
}).recursive.includingHidden {
    
}

文件系统 URL 资源

使用 URL.resources 获取文件系统资源的属性(如创建日期、文件大小或 Finder 标签)

let creationDate = fileURL.resources.creationDate
let fileSize = fileURL.resources.fileSize
let finderTags = fileURL.resources.finderTags

MeasureTime

测量执行代码块的时间。

MeasureTime.printTimeElapsed() {
/// The block to measure
}

NSObject 类反射

反射 NSObject 类的所有属性、方法和 ivar,包括隐藏的那些。

/// All properties, methods and ivars of `NSView`:
Swift.print(NSView.classReflection())

/// All class properties of `UIView`:
Swift.print(UIView.classReflection().classProperties)

OSHash

OpenSuptitle 哈希的实现。

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

Progress 扩展

progress.addFileProgress(url: fileURL, kind: .downloading)

使用 Blocks 进行通知观察

使用 block 观察 NotificationCenter 通知。

使用 NotificationCenterobserve(name:object:block:)。它返回 NotificationToken`,只要你想观察通知,就需要保存它。

let viewFrameNotificationToken = NotificationCenter.default.observe(NSView.frameDidChangeNotification, object: view) { _ in 
}

更多…