ExtendedAttributes
这个库通过扩展 URL
结构处理文件的扩展属性。
需求
- Swift 4.0 或更高版本
- macOS, iOS, tvOS 或 Linux
- XCode 9.0
安装
首先您必须从 GitHub 克隆此项目
git clone https://github.com/amosavian/ExtendedAttributes
然后,您可以通过将 Sources/ExtendedAttributes
目录添加到您的项目中或创建一个 xcodeproj
文件并将其添加为动态框架来手动安装
swift package generate-xcodeproj
用法
扩展属性仅与以 file:///
开头的 URL 一起使用。
列出
要列出文件设置的扩展属性
do {
print(try url.listExtendedAttributes())
} catch {
print(error.localizedDescription)
}
检索
检查是否存在特定的扩展属性
if url.hasExtendedAttribute(forName: "eaName") {
// Do something
}
要检索扩展属性的原始数据,请简单使用此代码作为模板,请注意,如果扩展属性不存在,它将引发错误。
do {
let data = try url.extendedAttribute(forName: "eaName")
print(data as NSData)
} catch {
print(error.localizedDescription)
}
如果您使用标准 plist 二进制格式设置扩展属性,可以检索其值。这些可以是 String
,Int
/NSNumber
,Double
,Bool
,URL
,Date
,Array
或 Dictionary
。数组不应包含 nil
值。
要检索扩展属性的原始数据,请简单使用此代码作为模板
do {
let notes: String = try url.extendedAttributeValue(forName: "notes")
print("Notes:", notes)
let isDownloeded: Bool = try url.extendedAttributeValue(forName: "isdownloaded")
print("isDownloaded:", isDownloeded)
let originURL: URL = try url.extendedAttributeValue(forName: "originurl")
print("Original url:", originurl)
} catch {
print(error.localizedDescription)
}
或列出文件的所有值
do {
for name in try url.listExtendedAttributes() {
let value = try url.extendedAttributeValue(forName: name)
print(name, ":" , value)
}
} catch {
print(error.localizedDescription)
}
设置属性
要设置扩展属性的原始数据
do {
try url.setExtendedAttribute(data: Data(bytes: [0xFF, 0x20]), forName: "data")
} catch {
print(error.localizedDescription)
}
要设置扩展属性的值
do {
let dictionary: [String: Any] = ["name": "Amir", "age": 30]
try url.setExtendedAttribute(value: dictionary, forName: "identity")
} catch {
print(error.localizedDescription)
}
移除
要移除扩展属性
do {
try url.removeExtendedAttribute(forName: "identity")
} catch {
print(error.localizedDescription)
}
已知问题
查看 问题页面。
贡献
我们非常欢迎您为 ExtendedAttributes 做出贡献,有关更多信息,请查看 LICENSE 文件。