How can I make an extension of a class that implements a protocol ?
Something like that :
protocol proto {
func hey()
}
and a class that conforms to proto
:
Class MyClass: UIViewController, proto {
func hey() {
print("Hey!")
}
}
and then an extension of that class that would look like :
extension UIViewController where Self:proto {
func test() {
print("I'm extended!")
}
}
So that I can call self.test()
in MyClass
.
Thanks.
An extension can extend an existing type to make it adopt one or more protocols. To add protocol conformance, you write the protocol names the same way as you write them for a class or structure: extension SomeType: SomeProtocol, AnotherProtocol {
Protocols let you describe what methods something should have, but don't provide the code inside. Extensions let you provide the code inside your methods, but only affect one data type – you can't add the method to lots of types at the same time.
A Swift extension allows you to add functionality to a type, a class, a struct, an enum, or a protocol.
You can just extend protocol, not the type. Please, try the following:
protocol proto {
func hey()
}
class MyClass: UIViewController, proto {
func hey() {
print("Hey!")
}
func test2() {
self.test()
}
}
extension proto where Self: UIViewController {
func test() {
print("I'm extended!")
}
}
First, you have to declare test
method in proto
so that MyClass
knows it implements this method.
protocol proto {
func hey()
func test()
}
Also you have to "reverse" the statements in the protocol extension:
extension proto where Self : UIViewController {
func test() {
print("I'm extended!")
}
}
After that, MyClass
is magically extended and you can call test
method on it:
class MyClass: UIViewController, proto {
override func viewDidLoad() {
super.viewDidLoad()
test()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With