How to add resources to your Swift Playground

Mohammed Awad-Allah
Code & Data
Published in
1 min readMay 3, 2015

Swift Playgrounds are sandboxed environments, meaning you cannot access resources outside of the playground package. So, In order to use a resource (a text file, an image … etc.) in your Swift Playground, you first need to have it inside your playground package.

In order to do so, you can follow these steps:

1. Open the file inspector

Choose: View > Utilities > Show File Inspector or alternatively press (alt+cmd+1)

2. Open Resources Folder

In the file inspector, under playground settings, you’ll find Resource Path. You can open the folder by clicking on the arrow icon beside the path.

3. Copy your files to the Resource Path

That’s it!

Example

The following snipped opens a text file from the resource path. (P.S. Don’t hard-code absolute path in your production code)

Originally published at m-medhat.tumblr.com.

--

--