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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:11:26  来源:igfitidea点击:

Swift Playground - Files are not readable

xcodeswiftxcode6foundationswift-playground

提问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:

示例图像:

XCode Terminal AppSwift Playground

XCode 终端应用程序斯威夫特游乐场

回答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 访问文件。如果您知道如何在沙箱外访问文件,欢迎您分享您的解决方案!!

Show Package Contents

显示包裹内容

Resources Folder

资源文件夹

回答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 LiteralFile MenuContext 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 letor varstatement. GIF here. enter image description here

这是最简单和最酷的。您只需将文件从机器上的任何地方拖到代码中,然后通过letorvar语句分配它。GIF在这里在此处输入图片说明

File Menu technique

文件菜单技术

Choose File -> Add Files to "MyProjectName"

选择 File -> Add Files to "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) 项目导航器中显示当前游乐场。显示当前的 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: enter image description here

我制作了一个动画 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 UIImageinstance is nilwhen you load an image file outside the sandbox. If you entered a path you know exists, but the image isn't showing, ensure the UIImageinstance 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!

并且任何位于那里的文件都可以被任何游乐场读取!