Swift Playground: "Expression are note allowed at the top level"

Hello everyone, I'm Teddy and just starting to learn Swift 5 with xcode 13.

I thought that by creating a Playground I could make short and simple codes to help my understanding of the language. However I get stuck on the same error message :

Expressions are not allowed at the top level

here is all of my code :

//
//  Main.swift
//  Swift-Playground
//

import Foundation

func greet(_ person: String, nicely: Bool = true) {
    if nicely == true {
        print("Welcome \(person) !")
    } else {
        print("Ooh no, it's \(person) again...")
    }
}

greet("teddy")

For the moment my playground only contains an assets and Main file.

What is missing in code to make simple tests ? Thanks.

Ok, I understood my mistake and I want to share it with you :

To be able to test simple lines of code in pure Swift, you must not create a playground via the xcode 13 home popup, but directly from the xcode menu bar:

File> New> Playground

So xcode will create a single Swift file instead of an entire SwiftUI-ready package.

import UIKit

var greeting = "Hello, playground"

Not sure what you mean by

 xcode 13 home popup

but the point is that effectively you need to create a playground, not a basic iOS project.

Swift Playground: "Expression are note allowed at the top level"
 
 
Q