Understanding the basic structure of a SwiftUI app – WeSplit SwiftUI Tutorial 1/11

Paul Hudson
7 Oct 202307:43

Summary

TLDRThis video script offers a detailed introduction to creating a new Swift UI project in Xcode. It explains the structure of the project, including the main files and their purposes, such as 'AppDelegate.swift' for app lifecycle management and 'ContentView.swift' for the initial user interface. The script delves into the Swift UI framework, highlighting the use of view modifiers and the 'VStack' for layout. It also introduces the concept of previews in Xcode, which allows developers to see a live version of their project and interact with it directly within the canvas, emphasizing the importance of this feature for design-time previews without affecting the final app.

Takeaways

  • 📁 When starting a new Swift UI project in Xcode, you get multiple files and code lines, with the Project Navigator on the left showing all the files.
  • 🚀 The 'App.Swift' file contains the main code to launch your app and maintain data while the app runs.
  • 💻 'ContentView.Swift' is where the initial user interface is coded and where all the project's coding will occur.
  • 🎨 The 'Assets' catalog is where you can add images, colors, app icons, and other visual elements for the app.
  • 🛠️ The 'Preview Content' group has its own asset catalog specifically for previewing data within Xcode.
  • 🔍 File extensions might be hidden depending on Xcode settings; you can change this in the General tab under settings.
  • 📜 The 'ContentView' struct conforms to the 'View' protocol, which is essential for displaying any data on the screen.
  • 🖼️ The 'body' property in 'ContentView' returns a view layout, which can include images, text, and other elements.
  • ⚙️ Swift UI's view modifiers, like 'padding' and 'foregroundStyle', allow you to modify views while returning new ones.
  • 🖥️ The 'Canvas' in Xcode allows you to preview your app's UI in real-time and is interactive, making it easier to test and adjust your design.

Q & A

  • What is the purpose of the 'WeitApp.swift' file in a new Swift UI project?

    -The 'WeitApp.swift' file contains the main code to launch the app and display the initial user interface. It also serves as the place to initialize and maintain data that should persist throughout the app's runtime.

  • Where do you write most of your code in a new Swift UI project?

    -Most of the coding for a new Swift UI project is done in the 'ContentView.swift' file, which contains the initial user interface for the app.

  • What is the role of the 'Assets' folder in an Xcode project?

    -The 'Assets' folder is the asset catalog where you can add pictures, colors, app icons, and other resources like iMessage stickers, which can be used in your app.

  • How can you preview your UI design in Xcode?

    -You can preview your UI design using the canvas in Xcode, which renders a live version of your project. The canvas can be enabled or disabled through the 'Editor' menu.

  • What is the 'SwiftUI' framework used for in a Swift UI project?

    -The 'SwiftUI' framework provides the functionality needed to create user interfaces in Swift. It includes tools for working with text, images, buttons, and more.

  • What does the 'ContentView' struct do in a Swift UI project?

    -The 'ContentView' struct defines the initial view of the app and conforms to the 'View' protocol, which is essential for displaying content on the screen.

  • What is the significance of the 'body' property in a Swift UI view?

    -The 'body' property in a Swift UI view is a computed property that returns some type of view conforming to the 'View' protocol. It dictates the layout and appearance of the view's content.

  • How does the 'View Modifier' work in Swift UI?

    -A 'View Modifier' in Swift UI is a method that returns a new view by applying modifications to the original data. For example, it can change an image's scale or add padding.

  • What is the purpose of the '#Preview' code in a Swift UI project?

    -The '#Preview' code allows you to render a live preview of your app's UI in Xcode. It doesn't affect the final app but is crucial for design-time visualization.

  • How can you change the device used in the Xcode canvas preview?

    -You can change the device in the Xcode canvas preview by selecting a different device from the list that appears when you click on the current device name. This will also change the simulator used when running the app.

Outlines

00:00

🚀 Overview of a New SwiftUI Project

When starting a new SwiftUI project, Xcode provides several files and around 20 lines of code. The project navigator on the left side of Xcode shows these files, including `App.swift` and `ContentView.swift`. `App.swift` contains the main code to launch the app and maintain data throughout its runtime. `ContentView.swift` holds the initial user interface, where most of the coding for the project will take place. Other files include `Assets.xcassets` for managing images, colors, and app icons, and `Preview Content` for previewing data in Xcode. The discussion also covers file extensions visibility in Xcode's settings.

05:02

🧩 Structure and Functionality in ContentView.swift

`ContentView.swift` is crucial as it defines the UI structure. The file begins with importing the SwiftUI framework, essential for UI components. It introduces the `ContentView` struct, which conforms to the `View` protocol, a fundamental protocol that all SwiftUI elements, like text, buttons, and images, adopt. The struct has a `body` property returning some kind of `View`. Inside the `body` is a `VStack`, which arranges elements vertically. The example discussed includes an image and a `Text` view, both modified using view modifiers like `imageScale`, `foregroundStyle`, and `padding`.

🎨 Utilizing Xcode’s Preview Feature

Below the `ContentView` struct, there’s a special preview section marked by `#Preview`, which doesn't affect the final app but is vital for previewing your UI in Xcode’s canvas. This canvas allows live previews of your project and is customizable for different Apple devices like iPhone and iPad models. If the canvas isn’t visible, it can be enabled via the editor menu in Xcode. The canvas is interactive, allowing you to make adjustments and see them reflected in real-time without running the full simulator. If the preview pauses due to an error, it can be refreshed with a keyboard shortcut.

Mindmap

Keywords

💡Swift UI

Swift UI is a framework by Apple for building user interfaces across all Apple platforms, including iOS, macOS, watchOS, and tvOS. It is designed to work seamlessly with Swift, Apple's programming language. In the context of the video, Swift UI is the primary tool used to create the user interface of the app, as indicated by the line 'import SwiftUI', which imports all the necessary functionality for the project.

💡Xcode

Xcode is Apple's integrated development environment (IDE) used for creating software for Apple platforms. It includes a suite of software development tools, such as a compiler, debugger, and interface builder. In the video, Xcode is the platform where the Swift UI project is created and managed, with the script mentioning the 'project Navigator' and 'canvas' features within Xcode.

💡ContentView

In Swift UI, ContentView is a struct that conforms to the View protocol and serves as the starting point for defining the user interface of an app. It is where developers write the code to display the initial layout and elements of the app. The script refers to 'ContentView.swift' as the file where all coding for the project takes place.

💡Asset Catalog

An asset catalog is a repository within Xcode that holds images, colors, and other resources used in an app. It helps manage and organize these assets for easy access and use within the app. The script mentions the 'assets' as the place to add pictures and colors for the app, as well as app icons and iMessage stickers.

💡View Protocol

The View protocol in Swift UI is a requirement for any piece of data that wants to be displayed on the screen. It is adopted by all UI elements, such as text, buttons, and images, and is the fundamental protocol for creating custom views. The script explains that 'ContentView' conforms to this protocol and must implement a 'body' property that returns some View.

💡Body Property

The body property is a computed property required by the View protocol in Swift UI. It defines the content and layout of a view and is the only mandatory property for a view struct. In the script, the 'body' property is mentioned as returning 'some View', which represents the UI elements that will be displayed on the screen.

💡VStack

VStack, short for vertical stack, is a view modifier in Swift UI that arranges other views vertically. It is used to create a column of UI elements. In the script, VStack is used to stack a globe image and 'Hello, World' text vertically, demonstrating how to layout elements in the app's interface.

💡SF Symbols

SF Symbols is a collection of over 1,800 consistent, highly configurable symbols that can be used in apps across all Apple platforms. They are part of Apple's design language and are available for free. The script mentions the globe image coming from SF Symbols, illustrating how to use these icons in the app's UI.

💡View Modifier

A view modifier in Swift UI is a function that alters the properties or behavior of a view without subclassing. They are used to apply styles or behaviors to views, such as padding, scaling, or changing the foreground color. The script describes the use of 'image scale', 'foreground style', and 'padding' as examples of view modifiers.

💡Canvas

The canvas in Xcode is a feature that allows developers to preview their Swift UI code in real-time. It provides a live rendering of the UI as it would appear on a device. The script explains the importance of the canvas for previewing the app's design and how it can be customized to display different devices.

💡Preview

In the context of Swift UI, a preview is a live rendering of the UI code that helps developers visualize and test their interface designs. The script discusses the 'PreviewProvider' and how it is used to generate previews within Xcode, which is crucial for design-time testing and iteration.

Highlights

Introduction to a new Swift UI project in Xcode, explaining the initial files and code structure.

Explanation of the `App.swift` file, which contains the main code to launch the app and show the initial user interface.

Description of `ContentView.swift` as the file where all coding for the project will take place.

Overview of the asset catalog in Xcode, where images, colors, app icons, and other assets can be managed.

Introduction to the preview content group, which allows for live previewing of data while coding in Xcode.

How to enable or disable file extensions in Xcode settings, helping users manage file visibility.

Explanation of the importance of comments in Swift code, describing how to add explanations without affecting code functionality.

Introduction to the `import SwiftUI` line, which imports all functionality from the SwiftUI framework.

Explanation of the `ContentView` struct, which conforms to the `View` protocol, allowing it to display UI elements on the screen.

Overview of the `body` property in SwiftUI, which defines the layout and appearance of the app's user interface.

Explanation of `VStack`, which organizes UI elements vertically within the SwiftUI layout.

Introduction to Apple's SF Symbols, a collection of icons available for use in apps across all Apple platforms.

Explanation of view modifiers in SwiftUI, which allow for modifications to views, such as adjusting image scale and foreground style.

Details about the preview feature in Xcode, which shows a live version of the project for design-time adjustments.

Tips on using the Xcode canvas for interactive previews, including troubleshooting when the canvas preview is paused.

Transcripts

play00:00

[Music]

play00:04

when you make a new Swift UI project

play00:06

you'll get a bunch of files plus 20 or

play00:08

so lines of code in X code on the left

play00:11

here is a project Navigator where you

play00:12

can see those files that been made for

play00:14

us here is weit app. Swift uh this

play00:18

contains the main code to launch your

play00:20

app and show your initial user interface

play00:23

uh if you want to make some data when

play00:24

the app starts and keep it alive the

play00:26

entire time while the app runs you put

play00:29

that here in the main abstruct here uh

play00:32

content. Swift contains the initial user

play00:34

interface for your app it's where we'll

play00:36

be doing all our coding for this project

play00:39

over here is assets this is our asset

play00:42

catalog where you can add pictures you

play00:44

want to use in the app also add colors

play00:46

here with app icons things like iMessage

play00:50

stickers and so much

play00:51

more then is a preview content group

play00:55

with its own asset catalog inside

play00:57

there's another one this time just for

play00:59

hand Ling previewing of data so you can

play01:01

see how things look while xcodes running

play01:05

now depending on whether you have X

play01:07

configured like me or something else

play01:09

perhaps here you might not see file

play01:11

extensions here on the left this is a

play01:13

swift file you can't tell that um but it

play01:16

is if you want to change that you can do

play01:17

under X codes options here under

play01:21

settings uh choose this General tab here

play01:24

and there a whole bunch of things

play01:25

options here and you're looking for file

play01:27

extensions and defaults hide all you

play01:29

might want show all um it's it's down to

play01:31

you anyway in this project all our code

play01:35

will take place inside this content

play01:37

view. Swift file here um this is open by

play01:40

default byx code when you make the

play01:42

project so you should see it very

play01:43

already open your screen um it has some

play01:46

comments at the top here uh the name of

play01:48

the file name of the project who made it

play01:50

and date and so forth um these are SED

play01:53

SL slash they're kind of grade out by

play01:54

default they're ignored by Swift so you

play01:57

can use these comments to add

play01:58

explanations to your code if you want to

play02:00

about how your code Works below that is

play02:04

15 or so lines of code this big chunk

play02:07

here before we start writing our own

play02:09

code it's really worth going through

play02:11

what all this does it's a couple of

play02:13

things we new to you first this import

play02:16

Swift UI line uh this tells Swift want

play02:19

to import all the functionality given to

play02:21

us by the Swift UI framework and apple

play02:24

rides Frameworks for a whole bunch of

play02:25

things like machine learning or audio

play02:28

playback or processing and and and so

play02:31

much more it's really powerful so rather

play02:33

than just assume our program wants to

play02:35

use everything ever we've got to tell it

play02:37

oh this is the part Swift UI that's what

play02:39

we want to use in our project here

play02:42

second this content view struct uh can

play02:44

call on view part here this makes a new

play02:48

struct called content view say it

play02:50

conforms to this view protocol and that

play02:52

view protocol comes with 50y and it's

play02:55

the basic protocol that every kind of

play02:57

piece of data that wants to draw to the

play02:59

screen somehow needs to adopt so all

play03:02

text all buttons all images sliders and

play03:04

so much more they're all views behind

play03:06

the scenes and when you have your own

play03:08

layouts that combine other views they're

play03:11

also views just get bigger and

play03:14

bigger inside there is this line here of

play03:16

our body Su view which makes a new

play03:18

computed property called body and it

play03:21

returns some View and what that means is

play03:25

it's going to return some kind of data

play03:26

that conforms to the view protocol which

play03:29

is our layout

play03:30

the things are show on the screen

play03:31

somehow and behind the scenes this will

play03:33

actually result in a very complicated

play03:35

type being returned oh it's a tabar then

play03:38

a text thing then to write that image

play03:39

and to write that slider big long exact

play03:43

typ our data but we don't want to say

play03:45

that we don't really care we're just

play03:47

saying trust me it'll be some kind of

play03:48

view that goes back now this view

play03:51

protocol has only one requirement which

play03:55

you have this computed body property

play03:57

here that returns some View uh you can

play04:00

and will add more properties and methods

play04:03

to your view structs but body is the

play04:05

only thing that's actually

play04:07

required inside the body is this big

play04:10

chunk of code here uh a vstack and it's

play04:12

going to show this globe image with

play04:15

hello world that's the image here and

play04:18

the text here uh this globe image comes

play04:21

from Apple's SF symbols icon collection

play04:24

where there are literally thousands of

play04:26

icons available to us in various weights

play04:28

and more and uh they're free to use in

play04:31

all your apps is really really nice they

play04:32

work on all Apple's platforms as well

play04:34

then this text view here is just simple

play04:36

text being drawn on the screen hello

play04:38

world it's not editable but it will wrap

play04:41

across multiple lines as needed it's

play04:42

just static text in the screen we also

play04:45

have a few interesting things here these

play04:47

methods being run on image here image

play04:50

scale foreground style and padding um

play04:54

this is what Swift UI calls a view

play04:56

modifier there are regular methods

play04:59

padding forr star and so forth but they

play05:02

have one small difference which is they

play05:03

always return a new view that contains

play05:06

both your original data your image or

play05:08

whatever but whatever extra modification

play05:11

you asked for oh it's now an image with

play05:13

a large image scale oh it's now an image

play05:15

with a tinted foreground style or

play05:18

whatever below this content view struct

play05:21

here is this hash preview line here with

play05:25

content view inside there's actually a

play05:26

special piece of code that won't

play05:28

actually form part of your final app

play05:30

that goes to the App Store but instead

play05:32

it's just to xcode so you can use that

play05:35

to say let's draw a preview of your code

play05:37

over here let's render a live version of

play05:39

your project over here and these preview

play05:42

form a really important X code feature

play05:43

called the canvas which is usually

play05:45

visible right in your X code window um

play05:47

you can customize cre if you want you

play05:49

can add more code into here to make it

play05:50

more unique into how much you want to um

play05:53

but it'll only affect the way your

play05:55

cameras is shown at design time when you

play05:57

ship to the App Store uh you know pushed

play06:00

your real phone for example you're

play06:01

testing things out it won't change the

play06:03

actual app being run it's just for

play06:04

previewing purposes only now the canvas

play06:07

will automatically preview using one

play06:09

specific Apple device I have the iPhone

play06:11

15 Pro Max right now the change is you

play06:14

want to see the list of device appear

play06:15

here aror appears click on that and

play06:18

choose something else you might say I

play06:20

want a iPad Mini or iPhone 15 or iPhone

play06:23

SE or something else here and when you

play06:24

do that it'll actually change the

play06:26

preview so it'll check okay I'll

play06:27

download the iPhone 15 Pro for example

play06:29

around the pro Max but it'll also change

play06:31

when the code runs so when you press uh

play06:33

command R and the virtual iOS simulator

play06:36

later on you'll get a different

play06:38

simulator now if you do not see this

play06:40

canvas um you'll have a hard time it's a

play06:42

really helpful thing to have here you

play06:44

want to go to the editor menu up here

play06:46

there's a whole bunch of options Only We

play06:47

Care at though is canvas right here that

play06:49

enables or disables X code's previewing

play06:51

canvas it's a good idea to have that

play06:54

available um because you can run code

play06:57

and see it straight away in there

play06:59

and it's actually interactive too see

play07:01

you can type directly into the canvas

play07:02

rather running in the simulator you can

play07:04

run it right in there too if you want to

play07:06

very often I should say sometimes you'll

play07:08

have an error in your code or something

play07:10

and you'll fix it and it'll it'll pause

play07:12

this previewing area which is

play07:14

frustrating you'll see a little refresh

play07:16

button where it says preview paused what

play07:18

you want to do is um either click the

play07:20

button to review the renew the preview

play07:22

or important keyboard shortcut option

play07:25

command p on your keyboard that will do

play07:28

the same thing as clicking the refresh

play07:42

button

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Swift UIXcode NavigationApp DevelopmentUser InterfaceSwift ProgrammingXcode TipsCoding GuidePreviewing AppsiOS DevelopmentProgramming Basics
Benötigen Sie eine Zusammenfassung auf Englisch?