user2636197 user2636197 - 1 month ago 8
Swift Question

swift should I use protocol or protocol :class

I have setup a protocal to send some information back to the previous VC.

I am defining 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
protocal?

Answer

: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]?) { }
}