Kiwo Tew
Kiwo Tew

Reputation: 1531

swift whats the difference between these protocols

I've seen protocols been declared in two ways, but I don't get the difference.

Ex1:

protocol AddItemViewControllerDelegate {
    func controller(controller: AddItemViewController, didAddItem: String)
}

Ex2:

protocol AddItemViewControllerDelegate: class {
    func controller(controller: AddItemViewController, didAddItem: String)
}

So whats the difference?

Upvotes: 0

Views: 63

Answers (2)

vadian
vadian

Reputation: 285150

From the docs:

You can limit protocol adoption to class types (and not structures or enumerations) by adding the class keyword to a protocol’s inheritance list. The class keyword must always appear first in a protocol’s inheritance list, before any inherited protocols:

protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
// class-only protocol definition goes here }

Note:

Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics.

Upvotes: 4

Justus Jianxing Zhang
Justus Jianxing Zhang

Reputation: 410

if you want to declare a variable like this :

var aVar : AddItemViewControllerDelegate?

you have to do :

protocol AddItemViewControllerDelegate: class {
    func controller(controller: AddItemViewController, didAddItem: String)
}

Upvotes: 1

Related Questions