这个Swift示例读取标记,添加标记Foo并将标记写回.
let url = NSURL(fileURLWithPath: "/Path/to/file.ext")var resource : AnyObject?do { try url.getResourceValue(&resource,forKey: NSURLTagnamesKey) var Tags : [String] if resource == nil { Tags = [String]() } else { Tags = resource as! [String] } print(Tags) Tags += ["Foo"] try url.setResourceValue(Tags,forKey: NSURLTagnamesKey)} catch let error as NSError { print(error)}
Swift 3版本有点不同.在URL中,tagnames属性是get-only,因此有必要将URL转换为Foundation NSURL
var url = URL(fileURLWithPath: "/Path/to/file.ext")do { let resourceValues = try url.resourceValues(forKeys: [.tagnamesKey]) var Tags : [String] if let tagnames = resourceValues.tagnames { Tags = tagnames } else { Tags = [String]() } Tags += ["Foo"] try (url as NSURL).setResourceValue(Tags,forKey: .tagnamesKey)} catch { print(error)}总结
以上是内存溢出为你收集整理的macos – 使用swift或obj-c在cocoa app OS X中读写文件标签全部内容,希望文章能够帮你解决macos – 使用swift或obj-c在cocoa app OS X中读写文件标签所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)