O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    34

Properties

You use properties to describe attributes of an object. To add a property to an object in Swift, you can add a variable or constant declaration to the type definition.

Note  Properties are not limited to class definitions in Swift. Enumerations and structures can also have property declarations.

You have already encountered property declarations in Chapter 31 with the declaration of the Person class (see Listing 34-1).

Listing 34-1. Property Declarations

class Person {    var name: String = "Name"    var age:Int = 0    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }}

In Listing 34-1, you declared two variables, name and age, with initial values. These types of properties ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required