简体   繁体   中英

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

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] ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM