Swift 2.0代码可在Xcode中使用,但不能在Playground中使用 [英] Swift 2.0 code works in Xcode but not in Playground
问题描述
我正在学习Swift,对试图弄清如何无法加载文件感到沮丧.事实证明,该代码可在Xcode中工作,但不能在游乐场中工作.是什么原因呢?
I'm learning Swift and have been frustrated trying to figure out how I'm unable to load files. It turns out that the code works in Xcode but does not work in a playground. What is the reason for this?
代码如下:
func testFileLoad(){
let myFilePath: String = "/Users/clay/Desktop/test.txt"
let t: Bool = NSFileManager.defaultManager().fileExistsAtPath(myFilePath)
print(t)
let s: String = try! String(contentsOfFile: myFilePath, encoding: NSUTF8StringEncoding)
print(s)
do {
let p: String = try String(contentsOfFile: myFilePath, encoding: NSUTF8StringEncoding)
print(p)
} catch {
print("nope")
}
}
在Xcode的测试模块中运行,它可以正常工作,并将我希望的内容输出到控制台.
Running in a test module in Xcode, it works properly and prints what I would hope for to the console.
Test Suite 'Selected tests' started at 2015-08-05 14:24:15.977
Test Suite 'swiftgraphTests' started at 2015-08-05 14:24:15.978
Test Case '-[swiftgraphTests.swiftgraphTests testFileLoad]' started.
true
this is a test
this is a test
Test Case '-[swiftgraphTests.swiftgraphTests testFileLoad]' passed (0.001 seconds).
Test Suite 'swiftgraphTests' passed at 2015-08-05 14:24:15.979.
Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'Selected tests' passed at 2015-08-05 14:24:15.979.
Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.002) seconds
在操场上,我得到了:
我在这里做错了什么?我在操场上使用不当吗?
What am I doing wrong here? Am I using the playground improperly?
推荐答案
如果转到菜单
查看"->调试区域"->显示调试区域"
"View" -> "Debug Area" -> "Show Debug Area"
您将看到完整的错误:您没有从Playground *访问文件系统的权限."
you will see the full error: "you don't have permissions to access the filesystem from the Playground*."
解决方法是使用项目浏览器将文件包含在游乐场中.
The workaround is to include the file in the Playground using its Project Navigator.
转到菜单
查看"->导航器"->显示项目导航器"
"View" -> "Navigators" -> "Show Project Navigator"
然后将文件拖放到资源"文件夹中.
then drag and drop your file into the "Resources" folder.
然后使用NSBundle获取路径.
Then use NSBundle to get the path.
func testFileLoad() {
// get the file path for the file from the Playground's Resources folder
guard let path = NSBundle.mainBundle().pathForResource("test", ofType: "txt") else {
print("Oops, the file is not in the Playground")
return
}
// keeping the examples from your question
let s: String = try! String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(s)
do {
let p: String = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(p)
} catch {
print("nope")
}
}
testFileLoad()
*实际上,您只能访问包含您的Playground共享数据的/var/
文件夹,而Playground只是提供了一个快捷方式.游乐场导航器中的该文件夹实际上代表/var/
文件夹,并且对于每个游乐场都是唯一的.您可以通过NSBundle查看其地址:
*Actually you have only access to the /var/
folder containing your Playground shared data, and the Playground is just offering a shortcut. This folder in the Playground's navigator actually represents the /var/
folder, and is unique for each Playground. You can see its address with NSBundle:
NSBundle.mainBundle().resourcePath
这篇关于Swift 2.0代码可在Xcode中使用,但不能在Playground中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!