Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Playground - Files are not readable

Files are not readable in Swift Playground.

How to make files readable?

Same code runs well on Xcode terminal app, but fails on 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

like image 494
D.A.H Avatar asked Nov 17 '14 15:11

D.A.H


People also ask

Is Swift Playgrounds 4 available?

Swift Playgrounds 4 now available.

Is Swift Playgrounds good for adults?

Simply put, after about an hour, even grown adults should be fully engaged by Swift Playgrounds' playful coding puzzles. That's a big win. Parents and children can go through these activities together, and they can both really get something out of it.

Can you use Swift Playgrounds on Windows?

Windows support has reached a stage where early adopters can now use Swift to build experiences for Windows, the project reports. Downloadable images of the Swift 5.3 toolchain for Windows 10 were introduced September 22.


2 Answers

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.

See Nate's answer here: 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 Resources folder

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

Show Package Contents

Resources Folder

like image 55
D.A.H Avatar answered Nov 11 '22 23:11

D.A.H


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

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. enter image description here

File Menu technique

Choose File -> Add Files to "MyProjectName"

See GIF of this here.

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).

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: enter image description here

Reading external code

If you want to add auxiliary code in addition to resources, use the Sources folder.

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.

like image 34
james_womack Avatar answered Nov 12 '22 01:11

james_womack