简体   繁体   English

如何在Swift 4.1中使用NSURL:setResourceValues()?

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

All the examples of using setResourceValues() are like this: 使用setResourceValues()的所有示例均如下所示:

var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? path?.setResourceValues(resourceValues)

However this doesn't compile with Swift 4.1, the error is 但是这不能用Swift 4.1编译,错误是

Cannot convert value of type 'URLResourceValues' to expected argument type '[URLResourceKey : Any]' 无法将类型“ URLResourceValues”的值转换为预期的参数类型“ [URLResourceKey:任何]”

How do I convert a URLResourceValues to a [URLResourcesKey:Any] ? 如何将URLResourceValues转换为[URLResourcesKey:Any]?

You are trying to call a method of URL on an instance of NSURL . 您试图在NSURL实例上调用URL方法。

NSURL defines: NSURL定义:

func setResourceValues(_ keyedValues: [URLResourceKey : Any]) throws

while URL defines: URL定义:

func setResourceValues(_ values: URLResourceValues) throws

Since you are using Swift, do not use NSURL . 由于您使用的是Swift,因此请勿使用NSURL Update your code so path is of type URL instead of NSURL and your call to setResourceValues will work. 更新您的代码,使pathURL类型而不是NSURL并且对setResourceValues的调用将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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