Podcast
Questions and Answers
What distinguishes stored properties from computed properties?
What distinguishes stored properties from computed properties?
- Stored properties store a value directly, while computed properties calculate a value. (correct)
- Stored properties are associated with the type itself, whereas computed properties are associated with instances of a particular type.
- Stored properties can only be constants, while computed properties can be variables.
- Stored properties are only available in classes, while computed properties are available in structures and enumerations.
Structures are reference types, so when an instance of a structure is assigned to a constant, its variable properties can still be modified.
Structures are reference types, so when an instance of a structure is assigned to a constant, its variable properties can still be modified.
False (B)
What keyword is used to declare a lazy stored property?
What keyword is used to declare a lazy stored property?
lazy
A computed property with a getter but no setter is known as a ______ computed property.
A computed property with a getter but no setter is known as a ______ computed property.
Match the property observer with its description:
Match the property observer with its description:
Which of the following is NOT a place where you can add property observers?
Which of the following is NOT a place where you can add property observers?
Property observers are called only when the new value of a property is different from its current value.
Property observers are called only when the new value of a property is different from its current value.
If a computed property's setter doesn't define a name for the new value to be set, what default name is used?
If a computed property's setter doesn't define a name for the new value to be set, what default name is used?
The keyword ______ is used to define type properties.
The keyword ______ is used to define type properties.
Match the term with its description:
Match the term with its description:
What is the primary purpose of a property wrapper?
What is the primary purpose of a property wrapper?
You can apply a property wrapper to a global variable.
You can apply a property wrapper to a global variable.
In a property wrapper, what is the name of the property that stores the wrapped value?
In a property wrapper, what is the name of the property that stores the wrapped value?
In addition to the wrapped value, a property wrapper can expose additional functionality by defining a ______.
In addition to the wrapped value, a property wrapper can expose additional functionality by defining a ______.
Match the term with the appropriate keyword:
Match the term with the appropriate keyword:
Which of the following is true regarding lazy stored properties?
Which of the following is true regarding lazy stored properties?
In Swift, a property always has a corresponding instance variable that serves as its backing store.
In Swift, a property always has a corresponding instance variable that serves as its backing store.
What syntax is used to access a type property?
What syntax is used to access a type property?
If you assign a value to a property within its own ______ observer, the new value that you assign replaces the one that was just set.
If you assign a value to a property within its own ______ observer, the new value that you assign replaces the one that was just set.
Match the structure with its description from the example:
Match the structure with its description from the example:
If you create an instance of a class and assign that instance to a constant, can you modify the instance's variable properties?
If you create an instance of a class and assign that instance to a constant, can you modify the instance's variable properties?
It is possible for both willSet
and didSet
observers to be called when assigning a new value to a property.
It is possible for both willSet
and didSet
observers to be called when assigning a new value to a property.
What is the name of the default parameter passed to the willSet
observer if a custom name is not specified?
What is the name of the default parameter passed to the willSet
observer if a custom name is not specified?
The name of a property wrapper's projected value is the same as the wrapped value, except it begins with a ______ sign.
The name of a property wrapper's projected value is the same as the wrapped value, except it begins with a ______ sign.
Match the initialization scenario with the SmallNumber initializer used:
Match the initialization scenario with the SmallNumber initializer used:
Why might a DataManager
class use a lazy stored property for its DataImporter
instance?
Why might a DataManager
class use a lazy stored property for its DataImporter
instance?
In a read-only computed property, you must include the get
keyword and its braces.
In a read-only computed property, you must include the get
keyword and its braces.
Using the example of AudioChannel
, what happens if a channel’s audio signal comes in with a value higher than thresholdLevel
?
Using the example of AudioChannel
, what happens if a channel’s audio signal comes in with a value higher than thresholdLevel
?
A Swift property does not have a corresponding ______ variable, and the backing store for a property isn't accessed directly.
A Swift property does not have a corresponding ______ variable, and the backing store for a property isn't accessed directly.
Match term and Usage
Match term and Usage
Given the SmallNumber
property wrapper and the struct SomeStructure
defined as:
`@propertyWrapper
struct SmallNumber {
private var number: Int
private(set) var projectedValue: Bool
var wrappedValue: Int {
get { return number }
set {
if newValue > 12 {
number = 12
projectedValue = true
} else {
number = newValue
projectedValue = false
}
}
}
init() {
self.number = 0
self.projectedValue = false
}
}
struct SomeStructure {
@SmallNumber var someNumber: Int
}
var someStructure = SomeStructure()`
What will be the output of print(someStructure.someNumber, someStructure.$someNumber)
after executing someStructure.someNumber = 15
?
Given the SmallNumber
property wrapper and the struct SomeStructure
defined as:
`@propertyWrapper struct SmallNumber { private var number: Int private(set) var projectedValue: Bool
var wrappedValue: Int {
get { return number }
set {
if newValue > 12 {
number = 12
projectedValue = true
} else {
number = newValue
projectedValue = false
}
}
}
init() {
self.number = 0
self.projectedValue = false
}
} struct SomeStructure { @SmallNumber var someNumber: Int } var someStructure = SomeStructure()`
What will be the output of print(someStructure.someNumber, someStructure.$someNumber)
after executing someStructure.someNumber = 15
?
A type can have both stored and computed type properties.
A type can have both stored and computed type properties.
In the context of property wrappers, what is the significance of the $
prefix?
In the context of property wrappers, what is the significance of the $
prefix?
According to the example, ______ are useful for defining values that are universal to all instances of a particular type.
According to the example, ______ are useful for defining values that are universal to all instances of a particular type.
Match the class/struct with its correct description
Match the class/struct with its correct description
Which of the following is true about stored type properties?
Which of the following is true about stored type properties?
Local variables cannot have property observers.
Local variables cannot have property observers.
What keyword allows subclasses to override the superclass’s implementation of a computed type property?
What keyword allows subclasses to override the superclass’s implementation of a computed type property?
When you access a projected value from code that’s part of the type, you can omit self.
before the property name, just like accessing other ______.
When you access a projected value from code that’s part of the type, you can omit self.
before the property name, just like accessing other ______.
Match the situation to the property type that will be most suitable:
Match the situation to the property type that will be most suitable:
Given the following code:
struct MyStruct {
static var myTypeProperty: Int = 5
var myInstanceProperty: Int = 10
}
var instance1 = MyStruct()
var instance2 = MyStruct()
instance1.myInstanceProperty = 20
MyStruct.myTypeProperty = 15
print(instance1.myInstanceProperty, instance2.myInstanceProperty, MyStruct.myTypeProperty)
What will be printed?
Given the following code:
struct MyStruct {
static var myTypeProperty: Int = 5
var myInstanceProperty: Int = 10
}
var instance1 = MyStruct()
var instance2 = MyStruct()
instance1.myInstanceProperty = 20
MyStruct.myTypeProperty = 15
print(instance1.myInstanceProperty, instance2.myInstanceProperty, MyStruct.myTypeProperty)
What will be printed?
The purpose of property observers is to add a layer of separation between managing a property its storage.
The purpose of property observers is to add a layer of separation between managing a property its storage.
What will happen if currentLevel is set to 15 in the following code, assuming AudioChannel.thresholdLevel
is 10?
struct AudioChannel {
static let thresholdLevel = 10
var currentLevel: Int = 0 {
didSet {
if currentLevel > AudioChannel.thresholdLevel {
currentLevel = AudioChannel.thresholdLevel
}
}
}
}
var channel = AudioChannel()
channel.currentLevel = 15
print(channel.currentLevel)
What will happen if currentLevel is set to 15 in the following code, assuming AudioChannel.thresholdLevel
is 10?
struct AudioChannel {
static let thresholdLevel = 10
var currentLevel: Int = 0 {
didSet {
if currentLevel > AudioChannel.thresholdLevel {
currentLevel = AudioChannel.thresholdLevel
}
}
}
}
var channel = AudioChannel()
channel.currentLevel = 15
print(channel.currentLevel)
Flashcards
Properties
Properties
Associate values with a class, structure, or enumeration.
Stored properties
Stored properties
Store constant or variable values as part of an instance.
Computed properties
Computed properties
Calculate a value instead of storing it.
Type properties
Type properties
Signup and view all the flashcards
Property Observers
Property Observers
Signup and view all the flashcards
Property Wrapper
Property Wrapper
Signup and view all the flashcards
Stored Property
Stored Property
Signup and view all the flashcards
Lazy Stored Property
Lazy Stored Property
Signup and view all the flashcards
Computed Properties
Computed Properties
Signup and view all the flashcards
Read-Only Computed Property
Read-Only Computed Property
Signup and view all the flashcards
Property Observers
Property Observers
Signup and view all the flashcards
Property Wrapper
Property Wrapper
Signup and view all the flashcards
Projected Value
Projected Value
Signup and view all the flashcards
Global Variables
Global Variables
Signup and view all the flashcards
Local Variables
Local Variables
Signup and view all the flashcards
Type Properties
Type Properties
Signup and view all the flashcards
Instance Variable
Instance Variable
Signup and view all the flashcards
Rects Center
Rects Center
Signup and view all the flashcards
Instance Properties
Instance Properties
Signup and view all the flashcards
Study Notes
- Properties associate values with classes, structures, or enumerations.
- Stored properties store values as part of an instance.
- Computed properties calculate a value instead of storing it.
- Classes and structures provide stored properties.
- Classes, structures, and enumerations provide computed properties.
- Type properties are associated with the type itself, not an instance.
- Property observers monitor changes in a property’s value.
- Property wrappers reuse code in the getter and setter of multiple properties.
Stored Properties
- The simplest form of a stored property is a constant or variable stored as part of a class or structure instance.
- Stored properties can be variable (using
var
) or constant (usinglet
). - Default values can be provided for stored properties.
- Initial values can be set or modified during initialization, even for constant stored properties.
- Instances of
FixedLengthRange
have a variable stored propertyfirstValue
and a constant stored propertylength
.
Stored Properties of Constant Structure Instances
- If a structure instance is assigned to a constant, its properties can't be modified, even if they are variable properties.
- This behavior is due to structures being value types.
- When a value type instance is marked as constant, all its properties are also constant.
- Classes are reference types, so variable properties of an instance assigned to a constant can still be changed.
Lazy Stored Properties
- A lazy stored property's initial value is not calculated until its first use.
- Use the
lazy
modifier before the property declaration. - Lazy properties are useful when the initial value depends on outside factors or requires complex setup.
DataImporter
takes a nontrivial amount of time to initialize.- The
importer
property is only created when first accessed, not when theDataManager
instance is created.
Stored Properties and Instance Variables
- Swift unifies properties and instance variables into a single property declaration.
- Properties don't have corresponding instance variables.
- The backing store for a property is not directly accessed.
- All property information is defined in one location within the type's definition.
Computed Properties
- Classes, structures, and enumerations can define computed properties that don't store a value.
- Computed properties provide a getter and an optional setter to indirectly retrieve/set other properties/values.
Rect
structure calculates the center from its origin and size, instead of storing it.- The
square
variable is initialized with an origin point of(0, 0)
, and a width and height of10
. - The
square
variable’scenter
property is then accessed through dot syntax (square.center
), which causes the getter forcenter
to be called, to retrieve the current property value, calculating and returning a newPoint
to represent the center of the square.
Shorthand Setter Declaration
- If a setter doesn't name the new value, the default name
newValue
is used.
Shorthand Getter Declaration
- If a getter's body is a single expression, it implicitly returns that expression.
- Omitting the
return
from a getter follows the same rules as omittingreturn
from a function.
Read-Only Computed Properties
- A computed property with only a getter is a read-only computed property.
- It returns a value but can't be set to a different value.
- The
get
keyword and its braces can be removed for simpler declaration.
Property Observers
- Property observers respond to changes in a property’s value.
- Called every time a property’s value is set, even if the new value is the same.
- Can be added to stored properties you define, stored properties you inherit, and computed properties you inherit.
- For computed properties that you define, use the property’s setter to observe and respond to value changes, instead of trying to create an observer.
willSet
is called just before the value is stored.didSet
is called immediately after the new value is stored.willSet
receives the new value asnewValue
(or a custom name).didSet
receives the old value asoldValue
(or a custom name).- Assigning a value to a property within its own
didSet
observer replaces the value that was just set.
Property Wrappers
- Property wrappers add a layer of separation between code that manages how a property is stored and the code that defines a property.
- A property wrapper defines a
wrappedValue
property. - Apply a wrapper to a property by writing the wrapper’s name before the property as an attribute (
@
). - The compiler synthesizes code for storage and access through the wrapper.
_height
and_width
properties store an instance of the property wrapper,TwelveOrLess
.- The getter and setter for
height
andwidth
wrap access to thewrappedValue
property.
Setting Initial Values for Wrapped Properties
- If no initial value is specified, Swift uses the
init()
initializer to set up the wrapper. - When an initial value is included for the property, Swift uses the
init(wrappedValue:)
initializer to set up the wrapper. - When arguments are written in parentheses, Swift uses the initializer that accepts those arguments.
Projecting a Value From a Property Wrapper
- A property wrapper can expose additional functionality by defining a projected value, with a name starting with
$
. - Writing
someStructure.$someNumber
accesses the wrapper’s projected value.
Global and Local Variables
- Capabilities for computing and observing properties are available to global and local variables.
- Global variables are defined outside any function, method, closure, or type context.
- Local variables are defined within a function, method, or closure context.
- You can apply a property wrapper to a local stored variable, but not to a global variable or a computed variable.
Type Properties
- Instance properties belong to an instance of a particular type.
- Type properties belong to the type itself, not any one instance.
- There is only one copy of type properties.
- Useful for values universal to all instances of a type.
- Stored type properties can be variables or constants.
- Computed type properties are always declared as variable properties.
Type Property Syntax
- Type properties are written as part of the type’s definition.
- Defined with the
static
keyword. - For computed type properties in classes, use
class
to allow subclasses to override.
Querying and Setting Type Properties
- Type properties are queried and set with dot syntax on the type itself, not an instance.
- If you set the
currentLevel
of the left channel to7
, you can see that themaxInputLevelForAllChannels
type property is updated to equal7
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.