Chapter 11: Properties

大綱

Stored properties

struct Contact {
  // provide a data type for each one but opt not to assign a default value, because you plan to assign the value upon initialization.
  var fullName: String
  let emailAddress: String
  var relationship = "Friend"
}

// Swift automatically creates an initializer for you based on the properties you defined in your structure
var person = Contact(fullName: "Grace Murray",
                     emailAddress: "grace@navy.mil",
                     relationship: "Friend")

// access the individual properties using dot notation
let name = person.fullName // Grace Murray
let email = person.emailAddress // grace@navy.mil

// assign values to properties as long as they’re defined as variables, and the parent instance is stored in a variable
person.fullName = "Grace Hopper"
let grace = person.fullName // Grace Hopper

// If you’d like to prevent a value from changing, you can define a property as a constant using "let"
person.emailAddress = "grace@gmail.com" // Error!

Default values

  • the automatic initializer doesn’t notice default values, so you’ll still need to provide a value for each property unless you create your own custom initializer

Computed properties

  • properties that are computed, which simply means they perform a calculation before returning a value

  • Computed properties must also include a type, because the compiler needs to know what to expect as a return value.

  • Computed properties don’t store any values; they return values based on calculations.

Getter and setter

Type properties

  • A type property is declared with the modifier static.

  • Using a type property means you can retrieve the same stored property value from anywhere in the code for your app or algorithm

Property observers

  • A willSet observer is called when a property is about to be changed while a didSet observer is called after a property has been changed.

  • willSet and didSet observers are only available for stored properties. If you want to listen for changes to a computed property, simply add the relevant code to the property’s setter.

  • the willSet and didSet observers are not called when a property is set during initialization

Limiting a variable

  • use property observers to limit the value of a variable

Lazy properties

  • If you have a property that might take some time to calculate, you don’t want to slow things down until you actually need the property. Say hello to the lazy stored property

Key points

  • Properties are variables and constants that are part of a named type.

  • Stored properties allocate memory to store a value.

  • Computed properties are calculated each time your code requests them and aren’t stored as a value in memory.

  • The static modifier marks a type property that’s universal to all instances of a particular type.

  • The lazy modifier prevents a value of a stored property from being calculated until your code uses it for the first time. You’ll want to use lazy initialization when a property’s initial value is computationally intensive or when you won’t know the initial value of a property until after you’ve initialized the object.

Last updated