Complete the code to declare a protocol named Printable.
protocol [1] {
func printDetails()
}The protocol is named Printable. This name matches the usage in the code.
Complete the code to make the struct conform to the Printable protocol.
struct User: [1] { var name: String func printDetails() { print("User: \(name)") } }
The struct User conforms to the Printable protocol by declaring it after the colon.
Fix the error by completing the method to conform to Printable.
struct Product: Printable {
var id: Int
func [1]() {
print("Product ID: \(id)")
}
}The method name must be printDetails() to match the protocol requirement.
Fill both blanks to create a protocol and make a class conform to it.
protocol [1] { func describe() } class Car: [2] { func describe() { print("This is a car.") } }
The protocol is named Describable and the class Car conforms to it by declaring Describable.
Fill all three blanks to define a protocol with a property and method, then conform a struct to it.
protocol [1] { var name: String { get } func greet() } struct Person: [2] { var name: String func [3]() { print("Hello, \(name)!") } }
The protocol is named Greetable. The struct Person conforms to it and implements the method greet().