Skip to content
FLAVIO COPES
flaviocopes.com
2026

SwiftUI: properties

By Flavio Copes

Learn how to add properties to a SwiftUI view and use them in your text, like a name constant interpolated into a Text view to greet the user.

~~~

You can add any property to any SwiftUI view, like this:

import SwiftUI

struct ContentView: View {
    let name = "Flavio"
    
    var body: some View {
        Text("Hello, \(name)!")
            .font(.largeTitle)
    }
}

Xcode showing SwiftUI code with name property and iPhone simulator displaying Hello, Flavio!

See how I used let because the property is a constant.

Note this, because later we’ll see how to update a property value by tapping a button.

Here is another example with an integer variable:

import SwiftUI

struct ContentView: View {
    let name = "Flavio"
    let age = 38
    
    var body: some View {
        VStack {
            Text("Hello, \(name)!")
                .font(.largeTitle)
            Text("You are \(age) years old")
        }
    }
}

Xcode showing SwiftUI code with name and age properties and iPhone simulator displaying Hello, Flavio! You are 38 years old

Tagged: Swift · All topics
~~~

Related posts about swift: