Swift Foundation 的扩展以及有用的类和实用工具。
要获得完整的文档,请查看位于/Documentation中的包含的文档。打开该文件会启动 Xcode 的文档浏览器。
数据大小的抽象表示。
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"
持续时间/时间间隔的抽象表示。
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)
observeChanges(for:)
观察 NSObject
的 KVO 属性。它返回 KeyValueObservation
,只要你想观察该属性,就需要保存它。
let textField = NSTextField()
let stringObservation = textField.observeChanges(for \.stringValue) { oldStringValue, stringValue in
/// stringValue changed
}
KeyValueObserver
观察多个属性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
添加额外的属性
// 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.resources
获取文件系统资源的属性(如创建日期、文件大小或 Finder 标签)
let creationDate = fileURL.resources.creationDate
let fileSize = fileURL.resources.fileSize
let finderTags = fileURL.resources.finderTags
测量执行代码块的时间。
MeasureTime.printTimeElapsed() {
/// The block to measure
}
反射 NSObject 类的所有属性、方法和 ivar,包括隐藏的那些。
/// All properties, methods and ivars of `NSView`:
Swift.print(NSView.classReflection())
/// All class properties of `UIView`:
Swift.print(UIView.classReflection().classProperties)
OpenSuptitle 哈希的实现。
let hash = try? OSHash.hash(url: fileURL)
hash?.hashValue /// The hash value
autoUpdateEstimatedTimeRemaining
:更新估计剩余时间和吞吐量。addFileProgress(url: URL)
:在 Finder 中显示文件进度。progress.addFileProgress(url: fileURL, kind: .downloading)
MutableProgress
:允许添加和删除子进度的一个进度。使用 block 观察 NotificationCenter
通知。
使用 NotificationCenter的
observe(name:object:block:)。它返回
NotificationToken`,只要你想观察通知,就需要保存它。
let viewFrameNotificationToken = NotificationCenter.default.observe(NSView.frameDidChangeNotification, object: view) { _ in
}
AsyncOperation
:一个异步的、可暂停的操作。PausableOperationQueue
:一个可暂停的操作队列。SynchronizedArray
/SynchronizedDictionary
:一个同步的数组/字典。Equatable.isEqual(_ other: any Equatable) -> Bool
:返回一个布尔值,指示该值是否与另一个值相等。Comparable.isLessThan(_ other: any Comparable) -> Bool
:返回一个布尔值,指示该值是否小于另一个值。