Recall & Review
beginner
What is a struct in Swift?
A struct is a way to group related data and functions together. It is a custom data type that can hold properties and methods.
Click to reveal answer
beginner
How do you declare a simple struct named
Person with a name property of type String?You write:<br>
struct Person {
var name: String
}Click to reveal answer
intermediate
Can structs in Swift have functions inside them? How?
Yes, structs can have functions called methods. You define them inside the struct like this:<br>
struct Person {
var name: String
func greet() {
print("Hello, \(name)!")
}
}Click to reveal answer
beginner
What keyword do you use to create a new instance of a struct?
You use the struct's name followed by parentheses with any required parameters. For example:<br><pre>let person = Person(name: "Alice")</pre>Click to reveal answer
intermediate
Are structs in Swift value types or reference types? What does that mean?
Structs are value types. This means when you assign or pass a struct, you get a copy, not a reference to the original. Changes to the copy do not affect the original.
Click to reveal answer
Which keyword is used to declare a struct in Swift?
✗ Incorrect
The keyword
struct is used to declare a struct in Swift.How do you define a property inside a struct?
✗ Incorrect
Properties inside structs are declared with
var or let followed by the property name and type, like var name: String.What happens when you assign one struct instance to another variable?
✗ Incorrect
Structs are value types, so assigning them creates a copy.
Which of these can a Swift struct contain?
✗ Incorrect
Structs can contain both properties (data) and methods (functions).
How do you create a new instance of a struct named
Car with a property model?✗ Incorrect
You create a new instance by calling the struct's initializer with the property name and value.
Explain how to declare a struct in Swift and include a property and a method.
Think about grouping data and behavior inside one block.
You got /4 concepts.
Describe the difference between structs and classes in Swift regarding value and reference types.
Focus on how data is copied or shared.
You got /4 concepts.