Questions tagged [swift-playground]

Swift Playgrounds are interactive design environments that evaluate code as you write it. Intermediate results of the code are shown in a right-hand pane.

Swift playgrounds are interactive documents where Swift code is compiled and run live as you type. Results of operations are presented in a step-by-step timeline as they execute, and variables can be logged and inspected at any point. Playgrounds can be created within an existing Xcode project or as standalone bundles that run by themselves.

Playgrounds provide a great opportunity to document functions and library interfaces by showing syntax and live execution against real data sets. For the case of the collection functions, we’ve created the CollectionOperations.playground, which contains a list of these functions, all run against sample data that can be changed live.

Playgrounds allow you to edit the code listings and see the result immediately.

The new playgrounds are especially useful for educators. You can insert rich instructional content with paragraph headings, diagrams, and links to additional material alongside the interactive Swift code.

New features in Xcode 6.3 playgrounds include the following:

  • Inline results display the output of your Swift code within the main editor window. The results area can be resized and configured to show different views of the output.

  • Stylized text is easy to add to your playground by adding special markup to your comments based on the familiar Markdown syntax. Some available styles are headings, bold, italic, lists, bullets, and links to external or bundled resources.

  • The Resources folder bundles images and other content directly within the playground. These resources can be accessed from your Swift code or from the rich comments within the playground. (Note: with Xcode 6.3 beta 2 use Show Package Contents in Finder to drag files into the playground’s Resources folder.)

Apple provides documentation here:

1311 questions
58
votes
4 answers

Debug breakpoint in Swift Playground?

I'm trying to add a breakpoint in the line # gutter, but no breakpoint is added when I do this in the playground. Is this possible or is there another way to set breakpoints in the playground?
TruMan1
  • 33,665
  • 59
  • 184
  • 335
47
votes
4 answers

Can Swift playgrounds see other source files in the same project?

I created the most simple custom class in a separate Swift file in my project: class Foo { init() { println("I made a foo.") } } Then, in a playground within the same project, I tried var x = Foo() Xcode didn't seem to like…
Maxwell Collard
  • 2,587
  • 3
  • 16
  • 12
45
votes
4 answers

Iterate through a String Swift 2.0

I am trying to do a very simple piece of code in Swift playgrounds. var word = "Zebra" for i in word { print(i) } However, I always get an error on line 3. 'String' does not have a member named 'Generator' Any ideas on why this doesn't work?…
Aaron
  • 1,312
  • 3
  • 14
  • 25
39
votes
10 answers

Playground Import: No Such Module 'Foo'

I've diligently followed the Apple instructions to import a custom module into a playground, including the instructions here. And yet I get: Playground execution failed:…
GoZoner
  • 67,920
  • 20
  • 95
  • 145
38
votes
5 answers

How to use cocoapods with playground?

I am trying out some pods before I implement it with my main project I want to make sure it works properly for my requirement. Easiest way is to try it with playground. I tried pod init with playground which doesn't work [!] No xcode project found,…
vinbhai4u
  • 1,329
  • 3
  • 19
  • 36
38
votes
6 answers

Making my function calculate average of array Swift

I want my function to calculate the average of my Double type array. The array is called "votes". For now, I have 10 numbers. When I call the average function to get the average of the array votes, it doesn't work. Here's my code: var votes = [1,…
Lukesivi
  • 2,206
  • 4
  • 25
  • 43
36
votes
2 answers

Could not find an overload for '/' that accepts the supplied arguments

// Playground - noun: a place where people can play func getAverage(numbers: Int...) -> Double{ var total = 0 var average:Double = 0 for number in numbers{ total = total + number } average = total / numbers.count …
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
33
votes
3 answers

How can I use Swift 2.3 in XCode 8 Playgrounds?

I have my playground project written in Swift 2.2 and I want take advantage of timeline visuals and try new debug features introduced in Xcode 8 beta. By default, Xcode 8 beta is using Swift 3 in Playgrounds and I cannot find a way to change that.…
33
votes
3 answers

No such module 'Cocoa' in Swift Playground

I'm trying to follow some of the code used in the WWDC playgrounds session, I'm trying to import the Cocoa framework with: import Cocoa But I get the following error in the assistant editor Playground execution failed: error: :3:8: error: no…
Charlie Egan
  • 4,878
  • 6
  • 33
  • 48
32
votes
2 answers

Use of undeclared type 'UIImage' in Swift

On using this function in swift and i am getting compiler error. Function is: class func imageWithImage (imageToResize : UIImage, scaledToSize newSize : CGSize) { return imageToResize; } and the error are: Use of undeclared type 'UIImage' Use…
Gaurav Pandey
  • 1,953
  • 3
  • 23
  • 37
30
votes
5 answers

Convert Character to Int in Swift 2.0

I just want to convert a character into an Int. This should be simple. But I haven't found the previous answers helpful. There is always some error. Perhaps it is because I'm trying it in Swift 2.0. for i in (unsolved.characters) { fileLines +=…
Aaron
  • 1,312
  • 3
  • 14
  • 25
28
votes
20 answers

Playground is not showing result on right bar

I waited long time but my playground project is not showing result on right side bar I am using right code but still right bar is not updating i don't know what is the problem i tried lot of time. here is my code var str = "Hello, playground" var…
Kishore Suthar
  • 2,943
  • 4
  • 26
  • 44
28
votes
2 answers

Xcode 6 - Prevent swift playground refresh

Sometimes I'm writing some code on a playground that takes a while to complete. For example: var values = [[Int]](count: 10000, repeatedValue: [Int](count: 73, repeatedValue: 0)) And everytime the playground wants to refresh the results, it has to…
Alex
  • 5,009
  • 3
  • 39
  • 73
27
votes
2 answers

Play musical notes in Swift Playground

I am trying to play a short musical note sequence with a default sine wave as sound inside a Swift Playground. At a later point I'd like to replace the sound with a Soundfont but at the moment I'd be happy with just producing some sound. I want…
Bernd
  • 11,133
  • 11
  • 65
  • 98
27
votes
5 answers

Array of functions in Swift

How can I store an array of functions to callback later in an array like in JavaScript? Any and AnyObject type cannot hold functions with different types of method signatures.
Encore PTL
  • 8,084
  • 10
  • 43
  • 78
1
2
3
87 88