0
0
Swiftprogramming~10 mins

Structs are value types (copy on assign) in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a struct named Point.

Swift
struct [1] {
    var x: Int
    var y: Int
}
Drag options to blanks, or click blank then click option'
APoint
BClass
CRectangle
DCircle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Class' instead of a struct name.
Choosing a shape name unrelated to the example.
2fill in blank
medium

Complete the code to assign one struct instance to another.

Swift
var p1 = Point(x: 5, y: 10)
var p2 = [1]
Drag options to blanks, or click blank then click option'
Ap1
BPoint(x: 0, y: 0)
CPoint()
Dp2
Attempts:
3 left
💡 Hint
Common Mistakes
Creating a new instance instead of copying.
Assigning the variable to itself.
3fill in blank
hard

Fix the error in copying and modifying a struct instance.

Swift
var p1 = Point(x: 3, y: 4)
var p2 = p1
p2.[1] = 10
Drag options to blanks, or click blank then click option'
Az
Bwidth
Cx
Dheight
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name not defined in the struct.
Trying to modify the original instance instead of the copy.
4fill in blank
hard

Fill both blanks to create a copy of a struct and change its property.

Swift
var original = Point(x: 1, y: 2)
var copy = [1]
copy.[2] = 5
Drag options to blanks, or click blank then click option'
Aoriginal
Bcopy
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying the original instead of the copy.
Using the wrong property name.
5fill in blank
hard

Fill all three blanks to demonstrate that modifying a copy does not affect the original struct.

Swift
var a = Point(x: 7, y: 8)
var b = [1]
b.[2] = 0
print(a.[3])
Drag options to blanks, or click blank then click option'
Aa
Bx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying the original struct instead of the copy.
Printing the wrong property.