Making Swift Playgrounds accessible

App Development

Hi all!
I just made an interesting observation, and thought it might help some of you.
As you might now, Apple introduced 'Playgrounds' in Xcode 6, which make it possible to run Swift code without having to compile it first. When I tested this out, I found that I could write code within a Playground, however I wouldn't be able to see any results using VO. At first I thought that this was simply inaccessible, but in fact this is not the case! Instead, I learned today that the Assistant Editor has to be visible in order to see the output of code. In order to make this visible in Xcode within a Playground, do this:
1. Create a Playground in Xcode or open an existing one.
2. Once you get to the editor, go to the menu bar by pressing VO+M and select the 'View' column.
3. Once you get there, press down arrow until you get to the 'Assistant Editor' sub menu.
4. Press enter to open the 'Assistant Editor' sub menu, and then select the 'Show Assistant Editor' option.

Once the Assistant Editor has been enabled and you stop interacting with the source code editor, you can press VO + right arrow to get to the Assistant Editor group. Interacting with this will reveal reveal a 'Console Output' text field, where the program's text output will be shown. For instance, a line of code like

println("Hello world!")

will show the text "Hello world!" on the screen. Furthermore, errors will also be shown here. Consider the following code:

import Cocoa

let myName = "John"
println("Hey there, my name is \(myName)!")
myName = "Robin"
println("Actually, my name is \(myName)...")

This will show the following error:

Playground execution failed: /var/folders/6f/5xnnw4px0m90_yz1vjd0jysh0000gn/T/lldb/13681/playground140.swift:7:8: error: cannot assign to 'let' value 'myName'
myName = "Robin"
~~~~~~ ^

This is because a constant (declared by using the 'let' keyword) cannot be assigned another value, and thus execution fails as can be seen by the error message. I find this better than navigating the 'Issue Navigator' within a complete Xcode project...
I hope this will help some of you! :-)
All the best,



Submitted by Mani on Thursday, January 5, 2017

I am trying to learn Swift and this was the first hurdle I came across. I chanced upon this post when I googled for a solution. Thank you so much, robin24.