xcode Swift Playground - 文件不可读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26976462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift Playground - Files are not readable
提问by D.A.H
Files are not readable in Swift Playground.
文件在 Swift Playground 中不可读。
How to make files readable?
如何使文件可读?
Same code runs well on Xcode terminal app, but fails on Swift Playground.
相同的代码在 Xcode 终端应用程序上运行良好,但在 Swift Playground 上运行失败。
Demo code below.
演示代码如下。
import Foundation
println("Hello, World!")
var fname:String = "/Users/holyfield/Desktop/com.apple.IconComposer.plist"
var fm:NSFileManager = NSFileManager.defaultManager()
if(fm.fileExistsAtPath(fname)){
println("File Exists")
if(fm.isReadableFileAtPath(fname)){
println("File is readable")
var fd:NSData? = NSData(contentsOfFile: fname)
println(fd?.length)
let pl = NSDictionary(contentsOfFile: fname)
println(pl?.count)
println(pl?.allKeys)
}else{
println("File is not readable")
}
}
else{
println("File does not exists")
}
Sample images:
示例图像:
回答by D.A.H
I have to thank Nate Cook for first for his quick response and solid answer.
我首先要感谢 Nate Cook 的快速反应和可靠的回答。
Just for case I share his answer from another post, which title is misleading.
以防万一,我从另一篇文章中分享了他的回答,该标题具有误导性。
See Nate's answer here: https://stackoverflow.com/a/26723557/2360439
在此处查看 Nate 的回答:https: //stackoverflow.com/a/26723557/2360439
Playgrounds are sandboxed, so you won't be able to just grab files from anywhere in your user folder. Here's how to add that file to your playground to make it accessible:
- Find your ".playground" file in the Finder Right click and choose "Show Package Contents"
- You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift"
- Make a new folder called "Resources" if it doesn't exists yet.
- Copy your files into Resourcesfolder
Playgrounds 是沙盒的,因此您将无法从用户文件夹中的任何位置抓取文件。以下是将该文件添加到您的 Playground 以使其可供访问的方法:
- 在 Finder 中找到您的“.playground”文件右键单击并选择“显示包内容”
- 您应该会看到“timeline.xctimeline”、“contents.xcplayground”和“section-1.swift”
- 如果尚不存在,请创建一个名为“ Resources”的新文件夹。
- 将您的文件复制到Resources文件夹中
Seems that there is no way to access files with Swift Playground outside of Playground sandbox. If you know how to access files outside of sandbox, you are welcome to share your solution!!
似乎无法在 Playground 沙箱之外使用 Swift Playground 访问文件。如果您知道如何在沙箱外访问文件,欢迎您分享您的解决方案!!
回答by james_womack
There are several ways to do load files into a Swift Playground. I'll highlight the Image Literal, File Menuand Context Menutechniques.
有几种方法可以将文件加载到 Swift Playground 中。我将重点介绍Image Literal、File Menu和Context Menu技术。
Image Literal technique
图像文字技术
This is the easiest and COOLEST. You simply drag a file from anywhere on your machine into the code and assign it via a let
or var
statement. GIF here.
这是最简单和最酷的。您只需将文件从机器上的任何地方拖到代码中,然后通过let
orvar
语句分配它。GIF在这里。
File Menu technique
文件菜单技术
Choose File -> Add Files to "MyProjectName"
选择 File -> Add Files to "MyProjectName"
Context Menu technique
上下文菜单技术
You can reveal the current playground in the Xcode (7.3.1) Project Navigator using the right-click menu. Upon revealing the current playground, you'll see a directory entitled Resources. Simply drag the file you want access to into that folder and watch the magic happen (given you've written the file access code).
您可以使用右键单击菜单在 Xcode (7.3.1) 项目导航器中显示当前游乐场。显示当前的 Playground 后,您将看到一个名为 Resources 的目录。只需将您想要访问的文件拖到该文件夹中,然后观察神奇的发生(假设您已经编写了文件访问代码)。
Example resource loading code
示例资源加载代码
Here's an example of doing this for showing an image:
这是显示图像的示例:
let url: NSURL! = NSBundle.mainBundle().URLForResource("IMG_4099", withExtension: "JPG")
let imagePath = url.path
let image = UIImage(contentsOfFile: imagePath!)
let imageView = UIImageView.init(image: image)
I've produced an animated GIFthat demonstrates this in action. Here's a screengrab from that:
我制作了一个动画 GIF来演示这一点。这是一个截图:
Reading external code
读取外部代码
If you want to add auxiliary code in addition to resources, use the Sources folder.
如果您想在资源之外添加辅助代码,请使用 Sources 文件夹。
A note regarding the console
关于控制台的说明
The console area of Xcode's Playground interface will show you that the UIImage
instance is nil
when you load an image file outside the sandbox. If you entered a path you know exists, but the image isn't showing, ensure the UIImage
instance isn't printing as nil
.
Xcode 的 Playground 界面的控制台区域将向您显示该UIImage
实例是nil
在您加载沙箱外的图像文件时。如果您输入了已知存在的路径,但图像未显示,请确保UIImage
实例未打印为nil
.
回答by OrbitalMan
You also can create and use Shared Playground Datafolder. Just like that:
您还可以创建和使用Shared Playground Data文件夹。就这样:
/Users/username/Documents/Shared Playground Data/file.txt
/Users/username/Documents/Shared Playground Data/file.txt
And any file located there becomes readable to any playground!
并且任何位于那里的文件都可以被任何游乐场读取!