Swift Playground - 文件不可读 [英] Swift Playground - Files are not readable

查看:64
本文介绍了Swift Playground - 文件不可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件在 Swift Playground 中不可读.

如何使文件可读?

相同的代码在 Xcode 终端应用上运行良好,但在 Swift Playground 上运行失败.

Same code runs well on Xcode terminal app, but fails on Swift Playground.

下面的演示代码.

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")
}

示例图片:

​​

推荐答案

首先要感谢 Nate Cook 的快速响应和可靠的答复.

I have to thank Nate Cook for first for his quick response and solid answer.

为了以防万一,我从另一个帖子中分享了他的回答,该标题具有误导性.

Just for case I share his answer from another post, which title is misleading.

在此处查看 Nate 的回答:https://stackoverflow.com/a/26723557/2360439

See Nate's answer here: https://stackoverflow.com/a/26723557/2360439

Playgrounds 是沙盒化的,因此您将无法从用户文件夹中的任何位置抓取文件.以下是如何将该文件添加到您的游乐场使其易于访问:

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:

  • 在 Finder 中找到您的.playground"文件,右键单击并选择显示包内容"
  • 您应该会看到timeline.xctimeline"、contents.xcplayground"和section-1.swift"
  • 创建一个名为Resources"的新文件夹(如果尚不存在).
  • 将您的文件复制到资源文件夹
  • 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 Resources folder

似乎无法在 Playground 沙箱之外使用 Swift Playground 访问文件.如果您知道如何在沙箱外访问文件,欢迎分享您的解决方案!!

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!!

这篇关于Swift Playground - 文件不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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