Reputation: 3
studying Protocols here and I'd like to know why that code does not print. Thanks in advance.
import Foundation
protocol PrintSomething {
func printMyname(name: String)
}
class Display {
var myProtocol: PrintSomething?
func somethingToDisplay() {
myProtocol?.printMyname(name: "Ygor")
}
}
let display = Display()
display.somethingToDisplay()
Upvotes: 0
Views: 70
Reputation: 2407
The documentation is pretty clear about what a protocol is:
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
For starters, you need an object to conform to your protocol which provides an actual implementation of the blueprint, because that is what a protocol is. Imagine you're building a house. You have the blueprint for it, but you still have to implement it in the real world.
protocol PrintSomething {
func printMyname(name: String)
}
class Display: PrintSomething {
func printMyname(name: String) {
print(name)
}
}
let display = Display()
display.printMyname(name: "My name")
If you wish to understand protocols in Swift I strongly recommend you begin with the documentation and start practicing the examples.
Upvotes: 1
Reputation: 136
You can use your code like below
protocol PrintSomething {
//declaration
func printMyname(name: String)
}
class Display: PrintSomething {
//defination
func printMyname(name: String) {
print(name)
}
var myProtocol: PrintSomething?
func somethingToDisplay() {
myProtocol?.printMyname(name: "Ygor")
}
}
And finally you can call it from any where like below
let display = Display()
display.somethingToDisplay()
You can read more about Protocol here
Upvotes: 1
Reputation: 4503
Because you never instantiated an actual instance of PrintSomething
. You have a reference to an optional which defaults to nil. Are you perhaps trying to have Display
conform to the protocol? You need an actual type to conform to the protocol and then instantiate that to be able to call it.
struct Printer: PrintSomething {
func printMyname(name: String) {
print(name)
}
}
class Display {
var myProtocol: PrintSomething = Printer()
func somethingToDisplay() {
myProtocol?.printMyname(name: "Ygor")
}
}
Now you have a real type (a struct
) implementing the protocol, you're instantiating it so that the reference isn't nil
, and then you're calling it.
You can do better, though, by having Display
implement the protocol directly.
class Display: PrintSomething {
func printMyname(name: String) {
print(name)
}
}
let display = Display()
display.printMyname(name: "whatever")
Upvotes: 1
Reputation: 51955
You have defined a property to be of a protocol type but you never assign something to that property and for that you need a concrete type, a struct or a class, to implement your protocol,
struct Example: PrintSomething {
func printMyname(name: String) {
print("This is my name: \(name)")
}
then you need to use it
let display = Display()
display.myProtocol = Example()
Now it will print when you call display.somethingToDisplay()
Upvotes: 0