Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 4: Properties & Methods

29. Stored Properties

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 28. Introduction Next episode: 30. Computed Properties

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

In the Swift Fundamentals course, you created a Student struct that had several properties. The name, grade, and pet were all “stored properties”. They were also instance properties. Which means their values would be stored with the Student instance they belong to.

32 struct Wizard {

}
33 var firstName: String
   var lastName: String
let wizard = Wizard(firstName: "Gandalf", lastName: "Greyjoy")
wizard.firstName
wizard.lastName
😺var 🛑wizard = 
wizard.firstName = "Hermione"
wizard.lastName = "Kenobi"
wizard
var firstName: String 😺{

}
34  willSet {
     
    }
    didSet {
      
    }
print(firstName + " will be set to " + newValue)
if firstName.contains(" ") {

}
print("No spaces allowed! \(firstName) is not a first name. Reverting name to \(oldValue).")
firstName = oldValue
wizard.firstName = "Merlin Rincewind"
let commonMagicalIngredients = [
  "Polyjuice Potion",
  "Eye of Haystack Needle",
  "The Force"
]
😺static 🛑let commonMagicalIngredients = [
Wizard.commonMagicalIngredients
static var commonMagicalIngredients = [
Wizard.commonMagicalIngredients😺.append("Wow-Wow Sauce")
    "The Force"
]😺 {
  didSet {
    print("Magical Ingredients updated! Common stock now contains \(commonMagicalIngredients)")
  }
}🛑