Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a struct named Person.
Swift
struct [1] {
var name: String
var age: Int
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of the struct name.
Forgetting to name the struct.
✗ Incorrect
The keyword Person is the name of the struct we want to define.
2fill in blank
mediumComplete the code to create an instance of the struct Person.
Swift
let person = [1](name: "Alice", age: 30)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' or 'struct' keywords instead of the struct name.
Trying to call a function that does not exist.
✗ Incorrect
To create an instance of a struct, use its name as a function.
3fill in blank
hardFix the error in the code to copy a struct instance correctly.
Swift
var person1 = Person(name: "Bob", age: 25) var person2 = person1 person2.name = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Assigning a variable name instead of a string.
✗ Incorrect
To assign a new name, use a string literal with quotes.
4fill in blank
hardFill both blanks to define a class and create its instance.
Swift
[1] PersonClass { var name: String init(name: String) { self.name = name } } let person = [2](name: "Dana")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' instead of 'class' to define a class.
Using the wrong name when creating an instance.
✗ Incorrect
Use class to define a class and the class name to create an instance.
5fill in blank
hardFill all three blanks to compare struct and class behavior.
Swift
var struct1 = Person(name: "Eve", age: 40) var struct2 = struct1 struct2.age [1] 41 var class1 = [2](name: "Frank") var class2 = class1 class2.name [3] "George"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=' for assignment.
Confusing struct and class names.
✗ Incorrect
Use = to assign new values. PersonClass is the class used. Structs copy values, classes share references.