Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, what does it mean for protocol to inherit from class keyword?

In Swift, what does it mean for protocol to inherit from class keyword?

e.g.

protocol MyDelegate: class {

}
like image 888
Boon Avatar asked Aug 20 '15 12:08

Boon


People also ask

CAN protocol inherit from class Swift?

A Swift protocol can inherit from other protocols, requiring conforming types to provide implementations for all the property and method requirements in the entire protocol hierarchy.

What is protocol inheritance in Swift?

One protocol can inherit from another in a process known as protocol inheritance. Unlike with classes, you can inherit from multiple protocols at the same time before you add your own customizations on top. Now we can make new types conform to that single protocol rather than each of the three individual ones.

What's the difference between a protocol and a class in Swift?

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.

What is class protocol in Swift?

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.


2 Answers

The gist of Starscream's answer is correct, but it misses the why which I think is important here. It comes down to ARC and memory management.

Swift is a language of reference types and value types. Classes are reference types, while everything else is a value type. Effectively, we're not really specifying that the protocol inherits from class... it's more like we're specifying that the protocol can only be implemented by reference types.

And why is that important?

It's important, because without it, we can't use the weak keyword with the protocol.

protocol ExampleProtocol {}

class DelegatedClass {
    weak var delegate: ExampleProtocol?
}

This generates the error:

'weak' cannot be applied to non-class type 'ExampleProtocol'

enter image description here

And why not? Because the weak keyword only makes sense with reference types to which ARC applies. ARC does not apply to value types. And without specifying our protocol with class, we cannot guarantee that our delegate property is set to a reference-type. (And if we don't use weak, we're most likely creating a retain cycle.)

like image 198
nhgrif Avatar answered Nov 25 '22 04:11

nhgrif


From the Apple 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.

Example:

protocol AProtocol: class {   
}

//Following line will produce error: Non-class type 'aStruct' cannot conform to class protocol 'AProtocol'
struct aStruct: AProtocol { 
}

The line declaring the structure will spit an error. Following line will produce error:

Non-class type 'aStruct' cannot conform to class protocol 'AProtocol'

like image 32
Starscream Avatar answered Nov 25 '22 04:11

Starscream