Skip to content

Protocols

Volker Enderlein edited this page Jan 12, 2020 · 2 revisions

You are here: HomeDocumentationMac information pageSc21 → Protocols

Protocols

For the uninitiated: A protocol specifies a collection of methods that have to be implemented by a class to "conform" to that protocol.

Protocols are a way of defining data types without having to use inheritance: instead of requiring an argument to be of a certain type (derived from a certain class), you require it to be able to respond to certain messages - "any object that can do X and Y".

Protocols are very widely used in Objective-C. For an example of protocol usage in Sc21, see the SCDrawable, SCView, and SCController source code.

Protocol conformance is declared like this (SCView conforms to the SCDrawable protocol):

@interface SCView <SCDrawable>

The data type would then be id<SCDrawable>, used like this:

- (void)setDrawable:(id<SCDrawable>)newdrawable;

back

Clone this wiki locally