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
-2
votes
2 answers

Why does the following swift code gives error?

The array does not append with a custom class object // Create an array with String and Double var shoppingList = ["Eggs", 2.0] // Append array with string object shoppingList += ["Milk"] // Declare an example class class Foo { var…
Vebz
  • 177
  • 1
  • 1
  • 8
-2
votes
2 answers

Trying to switch a direct integer value with a variable in swift

I'm trying to switch out a direct integer with a variable in swift, but for some reason I'm getting this error and I have no idea. The end goal is to get my currentValue (line 76) to replace the 100's on line 41 - could anyone let me know how I…
-2
votes
1 answer

ios sharedsession error on completion

I am blocked by this nasty error while learning iOS sharedSession singleton and async calls here is my code let baseUrl = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/") let forecast = NSURL(string: "47.856223,-122.272590",…
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
-2
votes
1 answer

Why does ("0"+"1").toInt()! return 1 as an Int, rather than 0

Was playing around with the reduce function on collections in Swift. //Reduce method should return an Int with the value 3141. let digits = ["3", "1", "4", "1"] .reduce(0) { (total:Int, digit:String) in return ("\(total)" +…
DanielRak
  • 239
  • 1
  • 2
  • 11
-3
votes
0 answers

Build failed because the Mach-O file couldn’t be generated using Swift Playground on an iPad

This happens to me every time I try to code using Swift Playgrounds on my iPad. I tried to make a music app for my online service. But it went wrong: Build failed because the Mach-O file couldn’t be generated
-3
votes
2 answers

How to call a function with a parameter

enter image description here func loveCaclculator(number: Int)-> Int { Int.random(in: 0..<1000) return number } loveCaclculator(number) print(loveCaclculator(number: 4)) I am getting an error "cannot find number in scope" in the debugger…
-3
votes
2 answers

Debug console prints the same random number for each loop iteration

I've written a simple code which appends a random number inside a loop. var randomNumber = Int.random(in: 0...3) var array = [Int]() for _ in 1...4 { array.append(randomNumber) } print(array) Instead of appending different numbers for each…
leopoldubz
  • 39
  • 4
-3
votes
1 answer

Number of decimals in a number in Swift

In the code below, I declared a variable by adding up different values depending on if some Toggles are true or false. When I try and print this variable it returns with a number and a single decimal after but it then is followed by a lot of 0 after…
Elias Fizesan
  • 265
  • 1
  • 3
  • 18
-3
votes
1 answer

How to create the Delegate Design Pattern within an Xcode Playground in Swift 5?

I encountered some kind of a problem while using a Swift Playground and trying to set up a simple Delegate Design. The problem seems to be the fact, that protocols can not be marked as public but the source folder in a Playground is considered as a…
christophriepe
  • 1,157
  • 12
  • 47
-3
votes
1 answer

Splitting a UInt16 to 2 UInt8 bytes and getting the hexa string of both. Swift

I need 16383 to be converted to 7F7F but I can only get this to be converted to 3fff or 77377. I can convert 8192 to hexadecimal string 4000 which is essentially the same thing. If I use let firstHexa = String(format:"%02X", a) It stops at 3fff…
CRey
  • 1,150
  • 2
  • 9
  • 21
-3
votes
1 answer

Type 'String.CharacterView.IndexDistance' (aka 'Int') does not conform to protocol 'Sequence'

I am attempting to make a morse code converter in a Swift 3 playground. The function I am using does not successfully initiate the translation process, saying "Type 'String.CharacterView.IndexDistance' (aka 'Int') does not conform to protocol…
Matthew McDonnell
  • 103
  • 1
  • 1
  • 5
-3
votes
1 answer

What's the mono-space font used on iPad's upcoming Swift Playgrounds app?

I was wondering if you guys know the name of the mono-space font on iPad's new coding app "Swift Playgrounds" that's coming out this fall. Here's the url to what the font looks like: http://www.apple.com/swift/playgrounds/. Thanks.
JS starter
  • 131
  • 2
  • 10
-3
votes
1 answer

Swift NStask function

I'm a complete swift noob. Using this code in xcode I get the result I need. I created a command line binary "menubar" that takes several arguments. I normally run it in the terminal "/bin/menubar getip", "/bin/menubar/getuser". I want to create a…
nyitguy
  • 598
  • 4
  • 18
-3
votes
1 answer

Alternate for main function in swift?

I was playing around in Playground And I wondered Where is Main Function unlike in other languages , Swift does not have a main function , Whats the alternative or technique used to replace main function in swift when using in architecture outside…
sriram hegde
  • 2,301
  • 5
  • 29
  • 43
-4
votes
1 answer

How to request keyboard input when a user presses a UIButton?

In Swift Playgrounds for iPad, how do you request keyboard input when a user presses a UIButton, then pass the input to a variable or container?
Colin FB
  • 110
  • 1
  • 7
1 2 3
87
88