Setting filesystem metadata from Swift

Hi,

I'm trying to convert the following code snippet to Swift:

Code Block
NSURL* pathURL = [NSURL fileURLWithPath:path];
BOOL success = [pathURL setResourceValue:tags forKey:NSURLTagNamesKey error:&outError];


However, it looks like the equivalent is now blocked in the API definition - in the Xcode 12 Swift API declaration .tagNames (and allValues) are get-only properties of URLResourceValues (according to StackOverflow, they were mutable in the past).

Code Block
extension URL {
    func setTagNames(_ tags: [String] ) {
        var values = URLResourceValues()
        values.tagNames = tags // Error here: Cannot assign to property: tagNames is get-only
        try self.setResourceValues( values )
    }
}


How can I set the tagNames?

Thanks,
Thomas



Seems Foundation team has changed the API without showing alternatives...

Can you try something like this?:
Code Block
extension URL {
mutating func setTagNames(_ tags: [String] ) throws {
self.setTemporaryResourceValue(tags, forKey: .tagNamesKey)
let values = try self.resourceValues(forKeys: [.tagNamesKey])
try self.setResourceValues(values)
}
}

(Sorry, I do not have enough time to explore now.)
It’s not at all clear why this has changed. Please file a bug about it and post your bug number, just for the record.

Notably, the underlying NSURL property, NSURLTagNamesKey, is still documented as read/write. This gives you a potential workaround, namely:

Code Block
func setTagNames(_ names: [String], for url: URL) throws {
let u = url as NSURL
try u.setResourceValue(names, forKey: .tagNamesKey)
}


Unfortunately I don’t have time to test this right now, so I’m not sure if this’ll actually work. Let us know how you get along.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks for your replies! Bug is filed: FB8910801

The NSURL workaround actually works, the temporaryResourceValue not.

The NSURL workaround actually works, the temporaryResourceValue not.

Thanks for testing and sharing the result. Seems using NSURL is the only workaround.
Hope this issue is fixed soon.

See this thread for the latest.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Setting filesystem metadata from Swift
 
 
Q