Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Swift上的setResourceValue时出现错误Domain=NSOSStatusErrorDomain代码=-5000_Swift - Fatal编程技术网

使用Swift上的setResourceValue时出现错误Domain=NSOSStatusErrorDomain代码=-5000

使用Swift上的setResourceValue时出现错误Domain=NSOSStatusErrorDomain代码=-5000,swift,Swift,我的问题很简单,我正在尝试使用Swift向Finder中的一些文件添加标记,我遇到了以下错误:error Domain=NSOSStatusErrorDomain code=-5000“afpAccessDenied:操作访问权限不足” 以下是我的功能: for product in fetchedObjects { let url = NSURL(fileURLWithPath: product.fileURLString!) print(pr

我的问题很简单,我正在尝试使用Swift向Finder中的一些文件添加标记,我遇到了以下错误:
error Domain=NSOSStatusErrorDomain code=-5000“afpAccessDenied:操作访问权限不足”

以下是我的功能:

    for product in fetchedObjects
    {
        let url = NSURL(fileURLWithPath: product.fileURLString!)

        print(product.fileNr)

        do
        {
            var tags = [String]()

            tags += ["test"]
            try url.setResourceValue(tags, forKey: URLResourceKey.tagNamesKey)
        }
        catch let error as NSError
        {
            print(error)
        }
    }

也许有人可以帮我?

错误消息表示您无权访问这些文件

如果需要向用户请求管理员权限,可以查看(在iOS 8+和MacOS 10.10+中提供)

使用具有生物特征的
设备所有者身份验证
,需要TouchID或FaceID。下面是同样的示例,允许用户使用其密码进行身份验证:

let myContext = LAContext()
let myLocalizedReasonString = <#String explaining why app needs authentication#>

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(. deviceOwnerAuthentication, localizedReason: myLocalizedReasonString) { success, evaluateError in
            if success {
                // User authenticated successfully, take appropriate action
            } else {
                // User did not authenticate successfully, look at error and take appropriate action
            }
        }
    } else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
    }
} else {
    // Fallback on earlier versions
}
让myContext=LAContext()
让MyLocalizedRealInstance=
var authError:NSError?
如果可用(iOS 8.0、macOS 10.12.1,*){
如果myContext.CaneValuePolicy(.deviceOwnerAuthenticationWithBiometrics,错误:&authError){
myContext.evaluatePolicy(.deviceOwnerAuthentication,localizedReason:myLocalizedReasonString){中的成功,evaluateError
如果成功{
//用户身份验证成功,请采取适当的操作
}否则{
//用户未成功进行身份验证,请查看错误并采取适当的操作
}
}
}否则{
//无法评估策略;请查看authError并向用户显示适当的消息
}
}否则{
//对早期版本的回退
}

问题似乎在于,我通过xcode启动的应用程序使用的是沙箱模式。在“功能”中将其关闭后,一切都像一个符咒一样工作。

要添加标记的文件路径是什么。尝试编辑下载或用户目录中的标记。我正在尝试访问位于桌面上的in文件夹,已经尝试在下载和用户目录中执行该功能。您是否在MacOS中执行此操作?该消息表示您没有添加标记的权限。检查文件权限:在Finder中,单击文件,然后按CMD-I或右键单击并选择“获取信息”。在终端中,在包含文件的目录中执行
ls-l
。是的,我正在Mac上执行此操作。我有读写权限,这就是为什么我对这个错误感到非常沮丧的原因。你的代码对我来说只需一个测试文件就可以完美地工作。仔细检查您的
产品.fileURLString
值:尝试在Finder中使用SHIFT-CMD-G,查看路径是否指向该文件。