Swift Playground - 文件不可读

时间:2021-12-28 11:03:13

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:

示例图片:

Swift Playground  - 文件不可读Swift Playground  - 文件不可读

2 个解决方案

#1


15  

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://*.com/a/26723557/2360439

请参阅Nate的答案:https://*.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"
  • 在Finder中找到“.playground”文件右键单击并选择“显示包内容”
  • You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift"
  • 你应该看到“timeline.xctimeline”,“contents.xcplayground”和“section-1.swift”
  • Make a new folder called "Resources" if it doesn't exists yet.
  • 如果它还不存在,请创建一个名为“Resources”的新文件夹。
  • Copy your files into Resources folder
  • 将文件复制到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访问文件。如果您知道如何访问沙箱之外的文件,欢迎您分享您的解决方案!

Swift Playground  - 文件不可读

Swift Playground  - 文件不可读

#2


7  

There are several ways to do load files into a Swift Playground. I'll highlight the Image Literal, File Menu and Context Menu techniques.

有几种方法可以将文件加载到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. Swift Playground  - 文件不可读

这是最简单和最酷的。您只需将文件从计算机上的任何位置拖到代码中,然后通过let或var语句进行分配。 GIF在这里。

File Menu technique

Choose File -> Add Files to "MyProjectName"

选择文件 - >添加文件到“MyProjectName”

See GIF of this here.

请参阅此处的GIF。

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)项目导航器中显示当前的游乐场。在揭示当前的游乐场后,您将看到名为“资源”的目录。只需将您想要访问的文件拖到该文件夹​​中,然后观察神奇的事情(假设您已经编写了文件访问代码)。

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 GIF that demonstrates this in action. Here's a screengrab from that: Swift Playground  - 文件不可读

我制作了一个动画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。

#1


15  

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://*.com/a/26723557/2360439

请参阅Nate的答案:https://*.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"
  • 在Finder中找到“.playground”文件右键单击并选择“显示包内容”
  • You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift"
  • 你应该看到“timeline.xctimeline”,“contents.xcplayground”和“section-1.swift”
  • Make a new folder called "Resources" if it doesn't exists yet.
  • 如果它还不存在,请创建一个名为“Resources”的新文件夹。
  • Copy your files into Resources folder
  • 将文件复制到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访问文件。如果您知道如何访问沙箱之外的文件,欢迎您分享您的解决方案!

Swift Playground  - 文件不可读

Swift Playground  - 文件不可读

#2


7  

There are several ways to do load files into a Swift Playground. I'll highlight the Image Literal, File Menu and Context Menu techniques.

有几种方法可以将文件加载到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. Swift Playground  - 文件不可读

这是最简单和最酷的。您只需将文件从计算机上的任何位置拖到代码中,然后通过let或var语句进行分配。 GIF在这里。

File Menu technique

Choose File -> Add Files to "MyProjectName"

选择文件 - >添加文件到“MyProjectName”

See GIF of this here.

请参阅此处的GIF。

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)项目导航器中显示当前的游乐场。在揭示当前的游乐场后,您将看到名为“资源”的目录。只需将您想要访问的文件拖到该文件夹​​中,然后观察神奇的事情(假设您已经编写了文件访问代码)。

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 GIF that demonstrates this in action. Here's a screengrab from that: Swift Playground  - 文件不可读

我制作了一个动画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。