Properties Flashcards

1
Q

Type Properties:

If you want to have create a struct named LevelTracker with a single constant type property maxLevel, what must you include in the struct?

A
Static 
static let maxLevel = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Type Properties:

What are type properties?

A

struct Point {
var x: Int = 0
var y: Int = 0
}

struct Map {
    static var origin = Point(x: 1, y: 1)
}
Map.origin = Point(x: 2, y: 2)

So basically there will only be one copy of this property, no matter how many instances of that type you create.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Computed Properties:

What is the difference between stored and computed properties?

A

Stored properties store constant and variable values as part of an instance.
- let length: Int

Computed properties calculate a value instead of storing it
var area: Int {
return length * width
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Computed Properties:

For the area property, this is a read only property. If you wanted to make it a read and right property, what would you have to use?

A

Get and set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If we don’t specify a constant to bind the value in a computed property’s setter, it is automatically bound to a variable named

A

newValue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Type properties are associated with the?

A

Type itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A computed property that does not specify a getter or setter is by default a

A

Read only computed property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Lazy Stored Properties:

What is a lazy stored property?

A

A property whose initial value is not calculated until the first time we use it.
Basically, it makes sure no computational power is wasted until it is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Lazy Stored Properties:

Why does a lazy stored property have to be stored as a var?

A

Because it might change when you eventually decide to use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Lazy Stored Properties:

What is the value of a lazy stored property before it is defined?

A

It does not have a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Property Observers:

What do property observers do?

A

Property observers allow us to observe and respond to changes in property values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Property Observers:

What are 2 examples of property observers?

A

willset and didset are property observers. - they allow us to observe changes in a properties value

willSet {
print(“Old value: (value)”)
}

    didSet {
        view.alpha = CGFloat(value)
        print("New value: \(value)")
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Property Observers:

When are willSet and didSet called?

A

didSet is executed after the value has been assigned.

willSet is called before the underlying value has been changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the purpose of us creating type properties?

A

There will only be one copy of this property, no matter how many instances of that type you create.

So it’s going to always have one value no matter what.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly