<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matteo Manferdini</title>
    <description>The latest articles on DEV Community by Matteo Manferdini (@matteom).</description>
    <link>https://dev.to/matteom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F227160%2Fbca4107b-4760-4397-a406-88a0ab52421e.jpg</url>
      <title>DEV Community: Matteo Manferdini</title>
      <link>https://dev.to/matteom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matteom"/>
    <language>en</language>
    <item>
      <title>Async/await in Swift and SwiftUI</title>
      <dc:creator>Matteo Manferdini</dc:creator>
      <pubDate>Tue, 12 Sep 2023 10:35:49 +0000</pubDate>
      <link>https://dev.to/matteom/asyncawait-in-swift-and-swiftui-2b8n</link>
      <guid>https://dev.to/matteom/asyncawait-in-swift-and-swiftui-2b8n</guid>
      <description>&lt;p&gt;&lt;em&gt;This article first appeared on &lt;a href="https://matteomanferdini.com/swift-async-await/"&gt;matteomanferdini.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1: What is async and await in Swift?
&lt;/h2&gt;

&lt;p&gt;Async/await is a mechanism used to create and execute asynchronous functions in Swift.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;async&lt;/strong&gt; indicates that a function or method is &lt;em&gt;asynchronous&lt;/em&gt; and can pause its execution to wait for the completion of another process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;await&lt;/strong&gt; marks a &lt;em&gt;suspension point&lt;/em&gt; in your code where execution may wait for the result of an async function or method.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to write an async function
&lt;/h3&gt;

&lt;p&gt;To declare an asynchronous function in Swift, write the &lt;code&gt;async&lt;/code&gt; keyword after the function name and before its return type.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Foundation

func fetchImageData() async throws -&amp;gt; Data {
    let data = // ... Download the image data ...
    return data
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Whenever one of your functions calls another asynchronous method, it must also be declared as &lt;code&gt;async&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use await in Swift
&lt;/h3&gt;

&lt;p&gt;You place the &lt;code&gt;await&lt;/code&gt; keyword wherever you need to call an &lt;code&gt;async&lt;/code&gt; function. It creates a suspension point where the execution of your code may pause until the asynchronous function or method returns.&lt;/p&gt;

&lt;p&gt;As an illustration, let’s download an image using a URL from &lt;a href="https://dog.ceo/dog-api/"&gt;the Dog API&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func fetchImageData() async throws -&amp;gt; Data {
    let url = URL(string: "https://images.dog.ceo/breeds/mountain-swiss/n02107574_1387.jpg")!
    let (data, _) = try await URLSession.shared.data(from: url)
    return data
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Chapter 2: Why do we need asynchronous functions in Swift and iOS apps?
&lt;/h2&gt;

&lt;p&gt;An iOS app idly waits for input, such as the user tapping a button or data arriving from the network. When such an input arrives, it triggers an event in the app that causes your code to run. After that, the user interface must be updated.&lt;/p&gt;
&lt;h3&gt;
  
  
  Blocking the main run loop for too long makes your app unresponsive
&lt;/h3&gt;

&lt;p&gt;When an iOS app runs, it consistently cycles through a run loop consisting of three phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Receiving input events.&lt;/li&gt;
&lt;li&gt;Executing code (possibly yours).&lt;/li&gt;
&lt;li&gt;Updating the UI.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The cycle runs so swiftly that the app appears to respond instantly to user input. However if some code takes too long to execute, it delays the subsequent UI update and input phases. Your app may feel sluggish, freeze briefly, and lose input.&lt;/p&gt;
&lt;h3&gt;
  
  
  Asynchronous functions can perform long tasks without blocking the app’s main run loop
&lt;/h3&gt;

&lt;p&gt;When you call an asynchronous function, your code gets suspended, leaving the app's main run loop free. Meanwhile, the work performed by the asynchronous function runs "in the background".&lt;/p&gt;

&lt;p&gt;The code running in the background can take as much time as it needs without impacting the app's main run loop. When it finishes and returns a result, the system resumes your code where it left off and continues executing it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Chapter 3: Structured and unstructured concurrency
&lt;/h1&gt;

&lt;p&gt;A task is a unit of work that can be run asynchronously.&lt;/p&gt;

&lt;p&gt;Tasks can be arranged hierarchically, allowing you to run several tasks in parallel. This approach is called &lt;em&gt;structured concurrency.&lt;/em&gt; The easiest way to create child tasks is by using &lt;code&gt;async let&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Swift also allows you to explicitly create and manage tasks. This approach, in turn, is called &lt;em&gt;unstructured concurrency&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Calling async functions sequentially
&lt;/h3&gt;

&lt;p&gt;We can create an async function to retrieve the URL for a random dog image and then download its data.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Dog: Identifiable, Codable {
    let message: String
    let status: String

    var id: String { message }
    var url: URL { URL(string: message)! }
}

func fetchDog() async throws -&amp;gt; Dog {
    let dogURL = URL(string: "https://dog.ceo/api/breeds/image/random")!
    let (data, _) = try await URLSession.shared.data(from: dogURL)
    return try JSONDecoder().decode(Dog.self, from: data)
}

func fetchImageData() async throws -&amp;gt; Data {
    let url = try await fetchDog().url
    let (data, _) = try await URLSession.shared.data(from: url)
    return data
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;fetchImageData()&lt;/code&gt; function makes two asynchronous calls sequentially.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running several asynchronous functions in parallel using structured concurrency
&lt;/h3&gt;

&lt;p&gt;You can run a discrete number of asynchronous functions simultaneously by using &lt;code&gt;async&lt;/code&gt; in front of &lt;code&gt;let&lt;/code&gt; when declaring a constant.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func fetchThreeDogs() async throws -&amp;gt; [Dog] {
    async let first = fetchDog()
    async let second = fetchDog()
    async let third = fetchDog()
    return try await [first, second, third]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;async let&lt;/code&gt; keywords do not create a suspension point like &lt;code&gt;await&lt;/code&gt;. We only use &lt;code&gt;await&lt;/code&gt; at the end of the function when we need the content of all three constants to create the final array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating unstructured tasks to call async methods from synchronous code
&lt;/h3&gt;

&lt;p&gt;To call async methods from synchronous code we run &lt;code&gt;async&lt;/code&gt; functions inside a &lt;code&gt;Task&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Task {
    let data = try await fetchThreeDogs()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code surrounding a task remains synchronous, so the main execution is not suspended.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Task&lt;/code&gt; type allows you to run async functions inside a Swift playground or a command-line Swift program. Far more common is calling async methods from SwiftUI.&lt;/p&gt;

&lt;h1&gt;
  
  
  Chapter 4 Using async/await in SwiftUI
&lt;/h1&gt;

&lt;p&gt;While it's not considered good practice to place async methods inside a SwiftUI view directly, many of these functions must still be triggered from SwiftUI code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling async methods when a SwiftUI view appears on screen
&lt;/h3&gt;

&lt;p&gt;SwiftUI specifically provides the &lt;code&gt;task(priority:_:)&lt;/code&gt; modifier for this purpose.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct ContentView: View {
    @State private var dogs: [Dog] = []

    var body: some View {
        List(dogs) { dog in
            // ...
        }
        .task {
            dogs = (try? await fetchThreeDogs()) ?? []
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The task modifier keeps track of the task it creates and automatically cancels it when the view disappears from the screen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling an async method when the user pulls to refresh or taps on a button
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;Task&lt;/code&gt; in the trailing closure of a &lt;code&gt;Button&lt;/code&gt;, or the trailing closure of the &lt;code&gt;refreshable(action:)&lt;/code&gt; view modifier.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct ContentView: View {
    @State private var dogs: [Dog] = []

    var body: some View {
        List(dogs) { dog in
            // ...
        }
        .refreshable {
            Task { 
                dogs = (try? await fetchThreeDogs()) ?? []
            }
        }
        .toolbar {
            Button("Reload") {
                Task { 
                    dogs = (try? await fetchThreeDogs()) ?? []
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Chapter 5: Async/await vs. completion closures
&lt;/h1&gt;

&lt;p&gt;Concurrency with async/await should be your primary choice for any new project. However, you might have an existing project using the old callback-based asynchronous approach.&lt;/p&gt;
&lt;h3&gt;
  
  
  Using callback-based asynchronous functions with completion closures
&lt;/h3&gt;

&lt;p&gt;Writing our &lt;code&gt;fetchDog()&lt;/code&gt; function using that approach is much more complicated than using async/await.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct HTTPError: Error {
    let statusCode: Int
}

func fetchDog(completion: @escaping (Result&amp;lt;Dog, Error&amp;gt;) -&amp;gt; Void) {
    let dogURL = URL(string: "https://dog.ceo/api/breeds/image/random")!
    let task = URLSession.shared.dataTask(with: dogURL) { data, response, error in
        if let error = error {
            completion(.failure(error))
            return
        }
        if let response = (response as? HTTPURLResponse), response.statusCode != 200 {
            completion(.failure(HTTPError(statusCode: response.statusCode)))
            return
        }
        do {
            let dog = try JSONDecoder().decode(Dog.self, from: data!)
            completion(.success(dog))
        } catch {
            completion(.failure(error))
        }
    }
    task.resume()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Replacing completion closures with async/await
&lt;/h3&gt;

&lt;p&gt;You can use continuations to wrap your old callback-based asynchronous functions and provide an async alternative instead of rewriting them all from scratch.&lt;/p&gt;

&lt;p&gt;Xcode also helps you in the process. ⌥-click on the function name, and you will find three options in the &lt;em&gt;Refactor&lt;/em&gt; contextual menu.&lt;/p&gt;
&lt;h3&gt;
  
  
  Convert Function to Async
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func fetchDog() async throws -&amp;gt; Dog {
    let dogURL = URL(string: "https://dog.ceo/api/breeds/image/random")!
    return try await withCheckedThrowingContinuation { continuation in
        let task = URLSession.shared.dataTask(with: dogURL) { data, response, error in
            if let error = error {
                continuation.resume(with: .failure(error))
                return
            }
            if let response = (response as? HTTPURLResponse), response.statusCode != 200 {
                continuation.resume(with: .failure(HTTPError(statusCode: response.statusCode)))
                return
            }
            do {
                let dog = try JSONDecoder().decode(Dog.self, from: data!)
                continuation.resume(with: .success(dog))
            } catch {
                continuation.resume(with: .failure(error))
            }
        }
        task.resume()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This option is helpful if you prefer to immediately replace all instances of the old function with the new Swift concurrency approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Async Alternative
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@available(*, renamed: "fetchDog()")
func fetchDog(completion: @escaping (Result&amp;lt;Dog, Error&amp;gt;) -&amp;gt; Void) {
    Task {
        do {
            let result = try await fetchDog()
            completion(.success(result))
        } catch {
            completion(.failure(error))
        }
    }
}


func fetchDog() async throws -&amp;gt; Dog {
    let dogURL = URL(string: "https://dog.ceo/api/breeds/image/random")!
    return try await withCheckedThrowingContinuation { continuation in
        // ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Select this option to retain all calls to the old version while using the new &lt;code&gt;async&lt;/code&gt; version in your new code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Async Wrapper
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@available(*, renamed: "fetchDog()")
func fetchDog(completion: @escaping (Result&amp;lt;Dog, Error&amp;gt;) -&amp;gt; Void) {
    let dogURL = URL(string: "https://dog.ceo/api/breeds/image/random")!
    let task = URLSession.shared.dataTask(with: dogURL) { data, response, error in
        // ...
    }
    task.resume()
}

func fetchDog() async throws -&amp;gt; Dog {
    return try await withCheckedThrowingContinuation { continuation in
        fetchDog() { result in
            continuation.resume(with: result)
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Utilize this option if you wish to maintain your existing code unchanged. It will keep using the old implementation while you incorporate Swift concurrency for new code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; &lt;a href="https://matteomanferdini.com/swift-async-await/"&gt;You can find the full version of this post here.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>programming</category>
      <category>networking</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Hello, World! Your First Swift Program</title>
      <dc:creator>Matteo Manferdini</dc:creator>
      <pubDate>Thu, 13 Oct 2022 14:27:22 +0000</pubDate>
      <link>https://dev.to/matteom/hello-world-your-first-swift-program-546n</link>
      <guid>https://dev.to/matteom/hello-world-your-first-swift-program-546n</guid>
      <description>&lt;p&gt;&lt;em&gt;This article first appeared on &lt;a href="https://matteomanferdini.com/swift-hello-world/" rel="noopener noreferrer"&gt;matteomanferdini.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Learning how to program is not an easy task. But it is straightforward to get started with it following a very simple Swift tutorial.&lt;/p&gt;

&lt;p&gt;The very first program everyone writes is called Hello World. It teaches you the basics of creating and running a program in any language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use the Hello World example to write your first Swift program&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first thing you need to learn is how to print something to the &lt;em&gt;console&lt;/em&gt;, an output text area where you can print any message.&lt;/p&gt;

&lt;p&gt;Printing to the console is straightforward, and it works in the same way in most programming languages. It also helps you understand how &lt;a href="https://en.wikipedia.org/wiki/Comment_(computer_programming)" rel="noopener noreferrer"&gt;comments&lt;/a&gt; work and teaches you how to fix basic errors.&lt;/p&gt;

&lt;p&gt;In this article, I will explore different ways of printing to the console in Swift by using the "Hello, world!" &lt;a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" rel="noopener noreferrer"&gt;example&lt;/a&gt;. This is the text used by most &lt;a href="https://en.wikipedia.org/wiki/Integrated_development_environment" rel="noopener noreferrer"&gt;programming environments&lt;/a&gt; when they generate templates for the apps you create.&lt;/p&gt;

&lt;p&gt;Apps that only show some text on the screen are the easiest to create and understand. You need to write more code as you develop advanced features for your apps, so it's essential to learn how to print to the console before moving on to anything else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create your first Hello World program using Xcode playgrounds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Playgrounds_(Xcode_feature)" rel="noopener noreferrer"&gt;playground&lt;/a&gt; is a file where Swift programs are written. You can do this in any text editor, but Xcode &lt;em&gt;playgrounds&lt;/em&gt; also allow you to run programs and see their results.&lt;/p&gt;

&lt;p&gt;Create a new playground by selecting &lt;em&gt;File -&amp;gt; New -&amp;gt; Playground...&lt;/em&gt; from the Xcode menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkq2d6j9rtli3e5y7nn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkq2d6j9rtli3e5y7nn9.png" alt="Creating a new playground in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose the &lt;em&gt;Blank template&lt;/em&gt; from the &lt;em&gt;iOS tab.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FXcode-playground-templates.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FXcode-playground-templates.png" alt="Xcode playground templates"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pick a name for the playground and choose where to save it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FSaving-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FSaving-an-Xcode-playground.png" alt="Saving an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The playground appears with some template code already inside.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FAn-Xcode-playground-with-the-standard-template-code.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FAn-Xcode-playground-with-the-standard-template-code.png" alt="An Xcode playground with the standard template code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it's time to write something. Delete everything in the playground and type the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There is only one instruction in this code, which tells the computer to print &lt;em&gt;"Hello World!"&lt;/em&gt; to the console.&lt;/p&gt;

&lt;p&gt;You don't know if everything works yet, so it is time to run the program and find out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run your first Hello World program in Xcode playgrounds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Press the play button in the bottom left corner of the playground's window to run the playground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-play-button-to-run-the-code-in-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-play-button-to-run-the-code-in-an-Xcode-playground.png" alt="The play button to run the code in an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Press the button in the bottom right corner of the playground's window on the same line as the play button to make the &lt;em&gt;debug area&lt;/em&gt; appear. You should see the playground's output printed in the debug area.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-debug-area-in-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-debug-area-in-an-Xcode-playground.png" alt="The debug area in an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also run the playground by pressing the play button in the gutter. This is the space on the left of the code where the line numbers are.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-editor-gutter-in-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-editor-gutter-in-an-Xcode-playground.png" alt="The editor gutter in an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also check the playground's output in the sidebar on the right side of the playground's window. The sidebar contains more information about the playground's output that isn't printed in the debug area when you run the playground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-sidebar-showing-the-intermediate-results-of-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-sidebar-showing-the-intermediate-results-of-an-Xcode-playground.png" alt="The sidebar showing the intermediate results of an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix your first Swift error in Xcode playgrounds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;print&lt;/code&gt; statement must follow the rules of the Swift syntax. Xcode detects &lt;a href="https://en.wikipedia.org/wiki/Syntax_error" rel="noopener noreferrer"&gt;errors&lt;/a&gt; as you type because the &lt;a href="https://en.wikipedia.org/wiki/Compiler" rel="noopener noreferrer"&gt;compiler &lt;/a&gt;complains if mistakes are made.&lt;/p&gt;

&lt;p&gt;The compiler stops when an &lt;em&gt;error&lt;/em&gt; occurs and displays an error message in the debug area at the bottom of the playground window.&lt;/p&gt;

&lt;p&gt;Remove a letter from the &lt;code&gt;print&lt;/code&gt; statement to see how this works in action.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello World!")
pint("Hello World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The compiler flags an error for the code. This happens because it doesn't recognize what you are trying to do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-running-code-in-an-Xcode-playground.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-running-code-in-an-Xcode-playground.png" alt="A syntax error when running code in an Xcode playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the missing letter to the &lt;code&gt;print&lt;/code&gt; instruction, and the error disappears. The compiler is happy now.&lt;/p&gt;

&lt;p&gt;This error is trivial, so it is straightforward to fix. Your code can still have mistakes if it's not written correctly, no matter how simple it is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Annotate your first Hello World program with comments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;comment&lt;/em&gt; is a piece of text in a program that the compiler ignores. You can use comments to annotate code. There are two ways to write comments in Swift:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Any text placed after the&lt;code&gt;//&lt;/code&gt;characters is a comment until the end of the line.&lt;/li&gt;
&lt;li&gt;You can delimit comments by starting with the&lt;code&gt;/*&lt;/code&gt;characters and ending with &lt;code&gt;*/&lt;/code&gt;. Any text between these delimiters is part of the comment, which allows the comment to span multiple lines.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is a comment, ignored by the compiler

print("Hello World!") // You can also write a comment next to an instruction

/* This is also a comment,
which spans multiple lines
and ends only with a
closing delimiter */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The hello world program is elementary, and it usually wouldn't need any comments. It is good practice to add comments only to code that is not immediately evident. You should use comments to explain why some code was written and not explain code the reader can easily understand.&lt;/p&gt;

&lt;p&gt;You can also make the code itself a comment to temporarily disable it. This is useful when you have some code that you don't want to run but may need to add or restore at a later stage.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// print("Hello world")
/*
print("Hello world")
print("Hello world")
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Create your first Hello World iOS app in SwiftUI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Apps are more complex than playgrounds but creating a hello world app is straightforward thanks to the default iOS app template that you get in Xcode.&lt;/p&gt;

&lt;p&gt;Create a new iOS app by selecting &lt;em&gt;File -&amp;gt; New -&amp;gt; Project...&lt;/em&gt; from the Xcode menu. Choose the &lt;em&gt;App&lt;/em&gt; template from the &lt;em&gt;Application _section in the _iOS&lt;/em&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FApp-templates-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FApp-templates-in-Xcode.png" alt="App templates in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the app settings that you should then select:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Name the app &lt;em&gt;hello world&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;em&gt;None&lt;/em&gt; for &lt;em&gt;Team&lt;/em&gt;. You don't need a development team for such a basic app.&lt;/li&gt;
&lt;li&gt;Enter the name of either your own company or the company you work for as the organization identifier. Xcode uses this to set the bundle identifier for the app.&lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;SwiftUI&lt;/em&gt; for the technology used to build the app's user interface. The other one is &lt;em&gt;UIKit&lt;/em&gt; which uses an entirely different approach with &lt;em&gt;storyboards&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;em&gt;Swift&lt;/em&gt; for the programming language used to code the app. This is your only option if you use SwiftUI for the user interface. UIKit also works with Objective-C in the background.&lt;/li&gt;
&lt;li&gt;Ensure that the Core Data and unit testing options are not selected. You don't need local databases to store the app's data since it's elementary. There is also no need to test the code of such a basic app.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FOptions-for-a-new-Xcode-project.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FOptions-for-a-new-Xcode-project.png" alt="Options for a new Xcode project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You need to perform quite a lot of configuration, even for a basic iOS app, because Xcode doesn't know what kind of app you are working on.&lt;/p&gt;

&lt;p&gt;Finally, check the &lt;em&gt;Create Git repository on my Mac&lt;/em&gt; for your project and save it. In our example, this will not make any difference, but it's useful to initialize a &lt;a href="https://en.wikipedia.org/wiki/Git" rel="noopener noreferrer"&gt;Git&lt;/a&gt; repository for any project you create since you will likely need it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FSaving-a-new-Xcode-project.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FSaving-a-new-Xcode-project.png" alt="Saving a new Xcode project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it's time to see what Xcode has created for you in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understand the structure of a basic SwiftUI iOS app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;hello world&lt;/em&gt; project has two source code files. You can choose either one of them from the project navigator on the left side of the Xcode editor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-project-navigator-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-project-navigator-in-Xcode.png" alt="The project navigator in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the &lt;em&gt;hello_worldApp.swift&lt;/em&gt; file and take a look at the code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There is quite a lot going on. &lt;a href="https://matteomanferdini.com/swiftui/" rel="noopener noreferrer"&gt;SwiftUI&lt;/a&gt; is a complex framework because it uses advanced Swift concepts such as opaque types, property wrappers, and result builders.&lt;/p&gt;

&lt;p&gt;You don't need to know about these concepts to understand how everything works. The most important thing you should be aware of is that you are using a &lt;em&gt;structure&lt;/em&gt;, which is a custom Swift data type, to define the main scene of the app.&lt;/p&gt;

&lt;p&gt;The scene handles a group of views of the app. There is only one view, in this case, declared in the &lt;em&gt;ContentView.swift&lt;/em&gt; file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The code uses a structure to define the only view of the app. The view contains everything that appears on the screen when you run the app.&lt;/p&gt;

&lt;p&gt;The easiest thing to show on the screen is a label with some text. This is the equivalent of printing something to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run your first iOS app in the simulator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three ways of running your iOS app: on an iPhone, using a &lt;a href="https://en.wikipedia.org/wiki/" rel="noopener noreferrer"&gt;simulator&lt;/a&gt;, and/or inside Xcode previews.&lt;/p&gt;

&lt;p&gt;You don't need an iPhone to run basic apps. A &lt;em&gt;simulator&lt;/em&gt; has all of the essential features of an actual device, and it is much easier to run an app using this.&lt;/p&gt;

&lt;p&gt;The first step is to choose the iOS simulator that you want to run the app on. A default simulator is already selected at the top of your Xcode project window. In this case, it is the iPhone 8.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-default-iOS-simulator-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-default-iOS-simulator-in-Xcode.png" alt="The default iOS simulator in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can click on the default simulator and choose a custom one from the simulator menu that appears.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-list-of-iOD-simulators-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-list-of-iOD-simulators-in-Xcode.png" alt="The list of iOD simulators in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-run-button-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-run-button-in-Xcode.png" alt="The run button in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project &lt;a href="https://en.wikipedia.org/wiki/Software_build" rel="noopener noreferrer"&gt;builds&lt;/a&gt; itself without errors and then runs the app on the chosen simulator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-hello-world-app-running-in-the-iOS-simulator.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-hello-world-app-running-in-the-iOS-simulator.png" alt="The hello world app running in the iOS simulator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SwiftUI also allows you to preview the app's appearance without running the app on a simulator. To do this, switch to the _ContentView.swift _file and look at the preview code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The code creates a structure that handles the updates of the app's main view to preview them. You can press the resume button at the top right of the preview window to preview the changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-resume-button-in-the-Xcode-canvas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-resume-button-in-the-Xcode-canvas.png" alt="The resume button in the Xcode canvas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The preview simulator is perfectly synchronized with the code of the view. This means that any changes you make to the code in the left panel can be seen in the simulator in the right panel immediately.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-hello-world-app-in-the-Xcode-canvas-1536x945.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-hello-world-app-in-the-Xcode-canvas-1536x945.png" alt="The hello world app in the Xcode canvas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create your first command-line macOS app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is quite a lot of boilerplate code in the iOS version of the &lt;em&gt;hello world&lt;/em&gt; app. This happens because you need to define a user interface for the app even though it is simple.&lt;/p&gt;

&lt;p&gt;To avoid all of this code, you can instead create a macOS version of the app, which only prints to the console and doesn't require a user interface of any kind whatsoever.&lt;/p&gt;

&lt;p&gt;You can create a new macOS app by selecting &lt;em&gt;File -&amp;gt; New -&amp;gt; Project...&lt;/em&gt; from the Xcode menu. Choose the &lt;em&gt;Command Line Tool&lt;/em&gt; template from the &lt;em&gt;Application _section in the _macOS&lt;/em&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-command-line-app-template-in-Xcode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-command-line-app-template-in-Xcode.png" alt="The command line app template in Xcode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name the app _hello world mac _and use the same settings for the app's development team, organization identifier, and programming language that you used for the iOS version of the app. Make sure you select Swift as the programming language of the app. The other options are Objective-C, C++, and C.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-Xcode-project-options-for-a-command-line-app.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-Xcode-project-options-for-a-command-line-app.png" alt="The Xcode project options for a command line app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, check the &lt;em&gt;Create Git repository on my Mac&lt;/em&gt; and save the project.&lt;/p&gt;

&lt;p&gt;All the code of the app is in the _main.swift _file. You simply use the &lt;code&gt;print&lt;/code&gt; statement to print a message to the console as you did when you used the Xcode playgrounds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can run the app in the very same way that you would run its iOS version.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-output-of-the-command-line-app-in-the-Xcode-console.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-output-of-the-command-line-app-in-the-Xcode-console.png" alt="The output of the command line app in the Xcode console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The app builds and runs on your Mac this time. It prints "Hello World" in the debug area at the bottom of the project window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run Swift code from the Terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The macOS version of the app is a &lt;a href="https://en.wikipedia.org/wiki/Console_application" rel="noopener noreferrer"&gt;console application&lt;/a&gt;, so you don't have to run it in Xcode only. Xcode provides a &lt;em&gt;read-eval-print loop&lt;/em&gt; (&lt;a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;) which is a &lt;a href="https://en.wikipedia.org/wiki/Command-line_interface" rel="noopener noreferrer"&gt;command-line interface&lt;/a&gt; that runs from the Terminal.&lt;/p&gt;

&lt;p&gt;Open the Terminal app and enter the &lt;code&gt;swift&lt;/code&gt; command to launch the REPL environment. This is what you should see in the console window if the command line tools are installed correctly in Xcode:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-swift-command-in-the-terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FThe-swift-command-in-the-terminal.png" alt="The swift command in the terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the Terminal doesn't recognize the previous command, then the Xcode command-line tools are not installed properly, so go ahead and install them before moving on.&lt;/p&gt;

&lt;p&gt;You can now run Swift code directly in the Terminal. Try out the &lt;code&gt;print&lt;/code&gt; statement again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FPrinting-in-the-Swift-REPL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FPrinting-in-the-Swift-REPL.png" alt="Printing in the Swift REPL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get errors if you type invalid code in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-in-the-Swift-REPL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-in-the-Swift-REPL.png" alt="A syntax error in the Swift REPL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can quit the Swift REPL by typing one of the following commands in the Terminal: &lt;code&gt;:exit&lt;/code&gt;, &lt;code&gt;:quit&lt;/code&gt; or &lt;code&gt;:q&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run Swift files from the Terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also run Swift files directly in the Terminal. Right-click the &lt;em&gt;hello world mac _folder, press the _Option&lt;/em&gt; key on your keyboard, and select the _Copy "hello world mac" as Pathname _option from the menu. You will need the folder's path on the disk to access it inside the Terminal.&lt;/p&gt;

&lt;p&gt;Next, enter the &lt;code&gt;cd [path]&lt;/code&gt; command in the Terminal to switch to the app's folder. Paste the project folder's path to replace the &lt;code&gt;[path]&lt;/code&gt; part of the command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-path-error-in-the-Terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-path-error-in-the-Terminal.png" alt="A path error in the Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Terminal does not work well with either file or folder path names that contain spaces. The easiest way to get around this is to place all the file and folder names with spaces between double quotes in the path's name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FEscaping-spaces-in-a-path-in-the-Terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FEscaping-spaces-in-a-path-in-the-Terminal.png" alt="Escaping spaces in a path in the Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can finally run the main Swift file of the macOS app in the Terminal. Type the &lt;code&gt;swift main.swift&lt;/code&gt; command in the Terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FRunning-a-Swift-command-line-app-in-the-Terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FRunning-a-Swift-command-line-app-in-the-Terminal.png" alt="Running a Swift command line app in the Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get errors if the file contains invalid Swift code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-running-a-Swift-command-line-app.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-running-a-Swift-command-line-app.png" alt="A syntax error when running a Swift command line app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create executable files from source files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use the Terminal to create executable files from source files. This is particularly useful when you want to share your work with someone else, but you don't want to give them access to your code.&lt;/p&gt;

&lt;p&gt;Access the &lt;em&gt;hello world mac&lt;/em&gt; folder in the Terminal the same way you did in the previous section.&lt;/p&gt;

&lt;p&gt;Enter the &lt;code&gt;swiftc main.swift&lt;/code&gt; command in the Terminal. This command creates an executable file with the same name and in the very same folder as the source file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FCreating-an-executable-file-for-a-Swift-command-line-tool.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FCreating-an-executable-file-for-a-Swift-command-line-tool.png" alt="Creating an executable file for a Swift command line tool"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can't create the executable file if the source file contains invalid Swift code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-creating-a-Swift-executable-file.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FA-syntax-error-when-creating-a-Swift-executable-file.png" alt="A syntax error when creating a Swift executable file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run the &lt;em&gt;main.exe&lt;/em&gt; executable file in the Terminal in two different ways.&lt;/p&gt;

&lt;p&gt;1. Double click the file in the &lt;em&gt;hello world mac&lt;/em&gt; folder, and it will automatically run in the Terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FExecuting-a-Swift-command-line-tool-by-double-clicking.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FExecuting-a-Swift-command-line-tool-by-double-clicking.png" alt="Executing a Swift command line tool by double clicking"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2. Type the &lt;code&gt;./main&lt;/code&gt; command in the Terminal to run the file manually.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FExecuting-a-Swift-command-line-tool-in-the-Terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmatteomanferdini.com%2Fwp-content%2Fuploads%2F2022%2F06%2FExecuting-a-Swift-command-line-tool-in-the-Terminal.png" alt="Executing a Swift command line tool in the Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
