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

Grunt

New Member
#1
All the examples of using setResourceValues() are like this:
Mã:
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] ?
 

Admin

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

NSURL defines:
Mã:
func setResourceValues(_ keyedValues: [URLResourceKey : Any]) throws
while URL defines:
Mã:
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.
 
Top