Skip to main content

ii. Hello, SwiftUI

About 5 minSwiftcrashcourseappleswiftxcode

ii. Hello, SwiftUI 관련


Hello, SwiftUI — Develop in Swift Tutorials | Apple Developer Documentation

Start your app development journey by getting to know Xcode, Swift, and SwiftUI.

Start your app development journey by getting to know Xcode, Swift, and SwiftUI.

In your first project, ChatPrototype, you’ll code a chat conversation using text views. The text and colors in the project are just suggestions, so feel free to make it your own by changing the words and style.


Create a new project

Begin by downloading Xcode, the tool you use to create apps.

Xcode manages all the files that make up an app: images, icons, project settings, and code.

Step 1

If you haven’t already done so, install Xcodeopen in new window from the Mac App Store.

Xcode is a large app, so you’ll need to give your computer time to install it.

Xcode
Xcode

Explore your project

Start by exploring three key features of Xcode: the navigator, the editor, and the preview.

Step 1

Step 1

The navigator is the leftmost column in the Xcode window. The Project navigator is selected. You can see its icon, a small folder, highlighted at the top of the pane.

The Project navigator displays all the files in your project. Note that ContentView is highlighted.


Explore your code

Read through the code. Swift and SwiftUI are designed to read like natural language.

Swift is the programming language you use to write your app’s code.

SwiftUI is a framework that provides views, controls, and layout structures for creating your app’s user interface.

Step 1

The names of many Swift elements are common English words.

Image, Text, and .imageScale all hint at how they create the interface.

Note

imageScale is in camel case: The words are glued together without spaces and each word after the first is capitalized.

import SwiftUI

struct ContentView: View {
  var body: some View {
    Vstack {
      Image(systemName: "globe")
        .imageScale(.large)
        .foregroundStyle(.tint)
      Text("Hello, world!")
    }
    .padding()
  }
}

#Preview {
  ContentView()
}

Edit code and make mistakes

Change the code in the editor and see how the preview updates in response.

Xcode has features common to other text editors, like copy and paste, undo and redo, and text selection.

Step 1

Change the text inside the quotation marks to a word or phrase of your choice.

Make sure not to delete the quotation marks themselves.

Note

You won’t see the preview until Xcode finishes downloading the iOS platform.

import SwiftUI

struct ContentView: View {
  var body: some View {
    Vstack {
      Image(systemName: "globe")
        .imageScale(.large)
        .foregroundStyle(.tint)
      Text("Knock, knock!")
    }
    .padding()
  }
}

#Preview {
  ContentView()
}

Use the library to add a Text view

In SwiftUI, a view is part of the interface of an app. Text is one example of a view.

Views are distinct from the data they display. Text is the view, and the String inside the parentheses is the data.

Step 1

Delete the line of code beginning with Image and the two lines below it. Then add a new line below the Text view.

import SwiftUI

struct ContentView: View {
  var body: some View {
    Vstack {
      Text("Knock, knock!")
      Text("Who's there?")
    }
    .padding()
  }
}

#Preview {
  ContentView()
}

이찬희 (MarkiiimarK)
Never Stop Learning.