0
0
Swiftprogramming~10 mins

Protocol conformance 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 protocol named Printable.

Swift
protocol [1] {
    func printDetails()
}
Drag options to blanks, or click blank then click option'
APrintableType
BPrintableProtocol
CPrint
DPrintable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different protocol name than Printable.
Adding extra words like 'Protocol' in the name.
2fill in blank
medium

Complete the code to make the struct conform to the Printable protocol.

Swift
struct User: [1] {
    var name: String
    func printDetails() {
        print("User: \(name)")
    }
}
Drag options to blanks, or click blank then click option'
APrintableProtocol
BPrintableType
CPrintable
DPrint
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong protocol name.
Forgetting to add the protocol after the colon.
3fill in blank
hard

Fix the error by completing the method to conform to Printable.

Swift
struct Product: Printable {
    var id: Int
    func [1]() {
        print("Product ID: \(id)")
    }
}
Drag options to blanks, or click blank then click option'
AprintDetails
BshowDetails
Cdisplay
DprintInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than required.
Missing the method implementation.
4fill in blank
hard

Fill both blanks to create a protocol and make a class conform to it.

Swift
protocol [1] {
    func describe()
}

class Car: [2] {
    func describe() {
        print("This is a car.")
    }
}
Drag options to blanks, or click blank then click option'
ADescribable
BPrintable
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for protocol and conformance.
Using the wrong protocol name.
5fill in blank
hard

Fill all three blanks to define a protocol with a property and method, then conform a struct to it.

Swift
protocol [1] {
    var name: String { get }
    func greet()
}

struct Person: [2] {
    var name: String
    func [3]() {
        print("Hello, \(name)!")
    }
}
Drag options to blanks, or click blank then click option'
AGreetable
Cgreet
DgreetDetails
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for protocol and conformance.
Incorrect method name implementation.