How To Use Xcode Playground Project Example

Xcode playground project is a project that can help you to run your swift source code immediately when you write it. This is something like what you see is what you get. It is also similar to python’s user interactive console. You write the code, then you see the result of the code’s execution. This article will give you some examples of how to use it.

1. How To Create Playground Project Use Xcode.

  1. When you start Xcode, there are three project templates that you can choose to create, click the first item Get started with a playground to create a playground project.
  2. Then it will pop up a playground project template window ( the window title is Choose a template for your new playground ), you can choose the playground os and template item in this window. For simplicity, we just choose macOS and the Blank template.
  3. Click the Next button, it will prompt you to save the playground file in a project folder. The playground file will use .playground file extension. After this, you can write swift code, run the code and see the result in the playground.

2. Write & Run Swift Source Code In Playground Project.

  1. Now copy below swift source code into HelloWorldPlayground.playground file.
    import Cocoa
    
    // declare a string variable with initial value.
    var str = "Hello, playground"
    
    // print above string variable.
    print(str)
    
    // declare a constant string variable and assign value to it.
    let user_name = "Tom"
    
    // insert above constant string variable in print text use \(constant_variable_name).
    print("Hello \(user_name), you are welcome to swift world")
    
    // create a constant rectangle object use NSRect class.
    let frame = NSRect(x:0, y: 0, width: 100, height: 100)
    
    // declare an Int variable with default value.
    var num:Int = 0;
    // loop 10 times. Use _ to replace i, because we do not use i in the for loop, so the xcode will advice you to use _, _ is a built-in variable which will be dropped automatically after use.
    for _ in 0...10
    {
        // num value plus 1.
        num+=1
        // print the num value.
        print(num)
    }
  2. Then click the run button (xcode-playground-run-button) at the beginning of each line to run the swift source code above the line ( not run the source code in the line ), then you can see the source code execution result in three places, 1). below the source code line, 2). in the console output at the window bottom, 3). in the playground project right panel.
    3-places-to-display-the-xcode-playground-execute-result
  3. If you can not click the start icon (xcode-playground-run-button) at swift source code line beginning, you will find there is a square button at the bottom console title, click this square button will stop the currently executed playground source code, then it will change to the start button, you can click it to run the playground source code again.
    click-the-square-button-to-stop-current-swift-playground-code-execution
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x