The Anatomy of a Basic SwiftUI Project

When a new SwiftUI project is created in Xcode using the Multiplatform App template, Xcode generates a number of different files and folders which form the basis of the project, and on which the finished app will eventually be built.

Although it is not necessary to know in detail about the purpose of each of these files when beginning with SwiftUI development, each of them will become useful as you progress to developing more complex applications. This chapter will provide a brief overview of each element of a basic Xcode project structure.

Creating an Example Project

If you have not already done so, it may be useful to create a sample project to review while working through this chapter. To do so, launch Xcode and, on the welcome screen, select the option to create a new project. On the resulting template selection panel, select the Multiplatform tab followed by the App option before proceeding to the next screen:

Figure 19-1

On the project options screen, name the project DemoProject. Click Next to proceed to the final screen, choose a suitable filesystem location for the project and click on the Create button.

Project Folders

SwiftUI is intended to allow apps to be developed which can, with minimal modification, run on a variety of Apple platforms including iOS, iPadOS, watchOS, tvOS and macOS. In a typical multiplatform project, there will be a mixture of code which is shared by all platforms and code which is specific to an operating system. In recognition of this, Xcode structures the project with a folder for the shared code and files together with folders to hold the code and files specific to iOS and macOS as shown in Figure 19-2. Additional folders may be added in which to place iPadOS, watchOS and tvOS specific code if needed:

Figure 19-2

The DemoProjectApp.swift File

The DemoProjectApp.swift file contains the declaration for the App object as described in the chapter entitled “SwiftUI Architecture” and will read as follows:

import SwiftUI
 
@main
struct DemoProjectApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

As implemented, the declaration returns a Scene consisting of a WindowGroup containing the View defined in the ContentView.swift file. Note that the declaration is prefixed with @main. This indicates to SwiftUI that this is the entry point for the app when it is launched on a device.

The ContentView.swift File

This is a SwiftUI View file that, by default, contains the content of the first screen to appear when the app starts. This file and others like it are where most of the work is performed when developing apps in SwiftUI. By default, it contains a single Text view displaying the words “Hello, world!”:

import SwiftUI
 
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
           .padding()
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Assets.xcassets

The Assets.xcassets folder contains the asset catalog that is used to store resources used by the app such as images, icons and colors.

Info.plist

The information property list file is an XML file containing key-value pairs used to configure the app. The setting to enable multi-window support, for example, is contained within this file.

Summary

When a new SwiftUI project is created in Xcode using the Multiplatform App template, Xcode automatically generates a number of files required for the app to function. All of these files and folders can be modified to add functionality to the app, both in terms of adding resource assets, performing initialization and de-initialization tasks and building the user interface and logic of the app. Folders are used to provide a separation between code that is common to all operating systems and platform specific code.