How to use NSURL:setResourceValues() in Swift 4.1?


New Member
All the examples of using setResourceValues() are like this:
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? path?.setResourceValues(resourceValues)
However this doesn't compile with Swift 4.1, the error is
Cannot convert value of type 'URLResourceValues' to expected argument type '[URLResourceKey : Any]'
How do I convert a URLResourceValues to a [URLResourcesKey:Any] ?


Thành viên BQT
You are trying to call a method of URL on an instance of NSURL.

NSURL defines:
func setResourceValues(_ keyedValues: [URLResourceKey : Any]) throws
while URL defines:
func setResourceValues(_ values: URLResourceValues) throws
Since you are using Swift, do not use NSURL. Update your code so path is of type URL instead of NSURL and your call to setResourceValues will work.

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.