无法打开文件,因为您没有查看错误的权限 [英] File couldn’t be opened because you don’t have permission to view it error

查看:382
本文介绍了无法打开文件,因为您没有查看错误的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了Google Stack Overflow,并对其进行了搜索,但似乎找不到解决方案.我有:

I have Googled and poked around Stack Overflow and can't seem to find a solution for this. I have:

let fileURL = URL( string: "file:///Users/me/file.txt" )    
var rawDataString: String 
var errorString: String?

do {
    rawDataString = try String( contentsOf: fileURL!, encoding: String.Encoding.utf8 )
} catch let error as NSError {
    errorString = error.description
    print( errorString! )
    return
}

并且错误提示

Error Domain = NSCocoaErrorDomain代码= 257文件"file.txt"无法 因为您没有查看权限而被打开."

Error Domain=NSCocoaErrorDomain Code=257 "The file "file.txt" couldn’t be opened because you don’t have permission to view it."

读取所有用户的权限:

$ ls -al file.txt
-rw-r--r--@ 1 me  staff  348306 Dec 13  2016 file.txt

任何想法都将受到欢迎.

Any ideas would be most welcome.

推荐答案

任何遇到此线程的人,@ LeoDabus都向我指出了在哪里关闭沙箱的方法,

Anyone coming across this thread, @LeoDabus pointed me to where to turn off sandbox, which worked:

他还整理了一下我的代码:

He also cleaned up my code a bit:

let fileURL = URL( fileURLWithPath: "/Users/me/file.txt" )    
var rawDataString: String
var errorString: String?

do {
    rawDataString = try String( contentsOf: fileURL, encoding: .utf8 )
} catch let error as NSError {
    errorString = error.description
    rawDataString = ""
    return
}

这篇关于无法打开文件,因为您没有查看错误的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆