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 with 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 example about how to use it.

1. How To Create Playground Project Use Xcode.

  1. When you start Xcode, there are three project template that you can choose to create, click the first item Get started with a playground to create a playground project.
    create playground project in xcode
  2. Then it will popup a playground project template window, you can choose the playground os and template item. For simplicity, we just choose macOS and Blank template.
    playground project template window for macOS
  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 place, 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 bottom console title, click this square button will stop current executed playground source code, then it will change to start button, you can click it to run the playground source code again.
    click the square button to stop current swift playground code execution
    run swift source code in playground
READ :   How To Set iOS UIView Background With Customized Color Or Image In Swift

Leave a Reply

Your email address will not be published. Required fields are marked *

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