0
0
iOS Swiftmobile~10 mins

Structs vs classes in iOS Swift - Interactive Practice

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

Complete the code to define a struct named Person with a name property.

iOS Swift
struct Person {
  var [1]: String
}
Drag options to blanks, or click blank then click option'
Aname
Bage
Cheight
Dweight
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated property names like age or height.
2fill in blank
medium

Complete the code to create an instance of the Person struct named person1.

iOS Swift
let person1 = [1](name: "Alice")
Drag options to blanks, or click blank then click option'
Astruct
Bclass
CPerson
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like class or var instead of the struct name.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to define a class named Animal.

iOS Swift
[1] Animal {
  var species: String
}
Drag options to blanks, or click blank then click option'
Aclass
Bfunc
Cstruct
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using struct or func instead of class.
4fill in blank
hard

Fill both blanks to complete the code that shows how classes support inheritance but structs do not.

iOS Swift
class Dog: [1] {
  var breed: String
  init(breed: String) {
    self.breed = breed
  }
}

struct [2] {
  var color: String
}
Drag options to blanks, or click blank then click option'
AAnimal
BPerson
CCar
DDog
Attempts:
3 left
💡 Hint
Common Mistakes
Using struct names that imply inheritance or using wrong base class names.
5fill in blank
hard

Fill all three blanks to complete the code that demonstrates value type behavior of structs and reference type behavior of classes.

iOS Swift
struct Point {
  var x: Int
  var y: Int
}

var p1 = Point(x: 0, y: 0)
var p2 = p1
p2.x = [1]

class Location {
  var name: String
  init(name: String) {
    self.name = name
  }
}

var loc1 = Location(name: "Home")
var loc2 = loc1
loc2.name = [2]

print(p1.x) // [3]
Drag options to blanks, or click blank then click option'
A10
B"Work"
C0
D"Home"
Attempts:
3 left
💡 Hint
Common Mistakes
Thinking structs behave like classes and share references.