I have setup a protocol to send some information back to the previous VC.
I define it like this:
protocol FilterViewControllerDelegate: class {
func didSearch(Parameters:[String: String]?)
}
But what is the difference when using:
protocol FilterViewControllerDelegate {
func didSearch(Parameters:[String: String]?)
}
And when should I use a : class
protocol?
It means that the protocol you define can be adopted only by classes, not structures or enums. In the example above, SomeClassOnlyProtocol can only be adopted by class types. It is a compile-time error to write a structure or enumeration definition that tries to adopt SomeClassOnlyProtocol.
You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.
In Swift, a protocol defines a blueprint of methods or properties that can then be adopted by classes (or any other types). Here, Greet - name of the protocol. name - a gettable property.
Protocol is used to specify particular class type property or instance property. It just specifies the type or instance property alone rather than specifying whether it is a stored or computed property. Also, it is used to specify whether the property is 'gettable' or 'settable'.
AnyObject
added to a protocol definition like this
protocol FilterViewControllerDelegate: AnyObject { func didSearch(parameters:[String: String]?) }
means that only a class will be able to conform to that protocol.
So given this
protocol FilterViewControllerDelegate: AnyObject { func didSearch(parameters:[String: String]?) }
You will be able to write this
class Foo: FilterViewControllerDelegate { func didSearch(parameters:[String: String]?) { } }
but NOT this
struct Foo: FilterViewControllerDelegate { func didSearch(parameters:[String: String]?) { } }
:class
added to a protocol definition like this
protocol FilterViewControllerDelegate: class { func didSearch(Parameters:[String: String]?) }
means that only a class will be able to conform to that protocol.
So given this
protocol FilterViewControllerDelegate: class { func didSearch(Parameters:[String: String]?) }
You will be able to write this
class Foo: FilterViewControllerDelegate { func didSearch(Parameters:[String: String]?) { } }
but NOT this
struct Foo: FilterViewControllerDelegate { func didSearch(Parameters:[String: String]?) { } }
There's also another thing about marking protocols with the class
/AnyObject
keyword.
Given a protocol like this:
protocol FilterViewControllerDelegate: AnyObject {
func didSearch(with parameters: [String: String]?)
}
protocol FilterViewControllerDelegate: class {
func didSearch(with parameters: [String: String]?)
}
For example, let's assume that you're creating a DetailViewController with delegate property of FilterViewControllerDelegate
type:
class DetailViewController: UIViewController {
weak var delegate: FilterViewControllerDelegate
}
If you didn't mark that protocol with class
keyword, you wouldn't be able to mark that delegate
property as a weak
one.
Why?
It's simple - only class based properties can have weak relationships. If you're trying to avoid a reference cycle, that's the way to go 😁
protocol FilterViewControllerDelegate: AnyObject {
func didSearch(Parameters:[String: String]?)
}
this protocol can be adopted by only classes.
To answer your first question -
But what is the difference when using:
the difference from this:
protocol FilterViewControllerDelegate {
func didSearch(Parameters:[String: String]?)
}
is that this protocol can adopt value types, such enums and structs as well.
To answer your second question -
And when should I use a : class protocal?
when you should use class protocol I would like to describe next example from delegate pattern: Imagine that you have delegate protocol.
protocol PopupDelegate: AnyObject {
func popupValueSelected(value: String)
}
and in another class you want to create a property
var delegate: PopupDelegate?
But this has strong reference that could bring you to problems with memory leaks. One way to fix memory leak is to make delegate property - weak. Until we will not make our protocol only available to apply for classes, Swift thinks we could apply our protocol also to value types.
weak var delegate: PopupDelegate?
If you try to declare your delegate like weak you will see next error:
'weak' var only be applied to class and class-bound protocol types, not 'PopupDelegate'
But we cant apply weak to value types. So we need to restrict our protocol to a reference type, so swift knows that its a reference type. To make you available to declare this delegate as weak you need to restrict your protocol to be used by classes only:
protocol PopupDelegate: AnyObject {
func popupValueSelected(value: String)
}
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