如何在Swift中为文件元数据写入Finder注释?
Swift 读写 Finder 注释:写入后无法在 Finder/mdls 中生效的问题
我尝试用 Swift 读写文件的 Finder 注释(该注释以二进制 plist 形式存储),读取注释的代码可以正常运行,但写入注释时,虽然自己的读取代码能读到变更,却无法在 Finder 里看到更新,用 mdls 命令也查不到变化。
读取代码:
let fileURL = URL(fileURLWithPath: "/path/to/file") guard let commentData = try? fileURL.extendedAttribute(forName: "com.apple.metadata:kMDItemFinderComment"), let commentString = try? PropertyListSerialization.propertyList(from: commentData, options: [], format: nil) as? String else { fatalError("Could not get comment") } print(commentString)
写入尝试代码:
let fileURL = URL(fileURLWithPath: "/path/to/file") let comment = "This is a Finder comment" do { let plistData = try PropertyListSerialization.data(fromPropertyList: comment, format: .binary, options: 0) try fileURL.setExtendedAttribute(data: plistData, forName: "com.apple.metadata:kMDItemFinderComment") } catch { fatalError("Could not write comment") }
内容的提问来源于stack exchange,提问作者user137369
相关产品推荐
相关产品推荐

