0
0
Swiftprogramming~10 mins

Struct declaration syntax 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 declare a struct named Person.

Swift
struct [1] {
    var name: String
}
Drag options to blanks, or click blank then click option'
Aperson
Bfunc
Cclass
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase for struct name.
Using keywords like class or func instead of a name.
2fill in blank
medium

Complete the code to add an integer property called age to the struct.

Swift
struct Person {
    var name: String
    var [1]: Int
}
Drag options to blanks, or click blank then click option'
Aage
BAge
Cnumber
Dyears
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase for property name.
Using unrelated names like years or number.
3fill in blank
hard

Fix the error in the struct declaration by completing the missing keyword.

Swift
[1] Person {
    var name: String
    var age: Int
}
Drag options to blanks, or click blank then click option'
Aclass
Bstruct
Cfunc
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using class instead of struct.
Omitting the keyword entirely.
4fill in blank
hard

Fill both blanks to declare a struct with a method that returns a greeting.

Swift
struct Person {
    var name: String
    func [1]() -> String {
        return "Hello, \(self.[2])!"
    }
}
Drag options to blanks, or click blank then click option'
Agreet
Bname
CsayHello
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that doesn't match the greeting purpose.
Referencing a property that doesn't exist.
5fill in blank
hard

Fill all three blanks to declare a struct with a computed property that returns a description.

Swift
struct Person {
    var name: String
    var age: Int
    var [1]: String {
        return "\(self.[2]) is \(self.[3]) years old."
    }
}
Drag options to blanks, or click blank then click option'
Adescription
Bname
Cage
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong property name for the computed property.
Mixing up property names inside the string.