Learn Swift from Objective-C : Protocols and Delegation

This tutorial is a continuation from Part 1 and Part 2 where we went through a comparison between Swift and Objective C for various operations involving variables, classes, methods and properties.

This tutorial is best suited for people who already know Objective-C (at least the basics) and want to see what the equivalents are in Swift.

If you’re a non-programmer and you’re just beginning your journey into iOS development, I’d recommend taking a look at my course where you’ll learn Objective-C as part of learning how to build iPhone apps. When Swift is released, as a member of the course, you’ll have access to my new Swift course for free.


In iOS development, a protocol is a set of methods and properties that encapsulates a unit of functionality. The protocol doesn’t actually contain any of the implementation for these things; it merely defines the required elements. Any class that declares itself to conform to this protocol must implement the methods and properties dictated in the protocol.

Declaring a protocol


In Objective-C, protocols are declared with the “@protocol” keyword. Below is an example of declaring a protocol containing one required method.


In Swift, the syntax is a little different but the idea is the same.

Conforming to a protocol


In Objective-C, you add the protocol name in angle brackets beside the class interface declaration.

MyClass is declaring that it conforms to SampleProtocol below. MyClass will also have to provide an implementation for “someMethod” in the implementation file because it is a required protocol method.


In Swift, the protocol name is appended beside the superclass name, separated with a comma. If there isn’t a superclass, then you just write a colon, followed by the protocol name.

Both MyClass and AnotherClass conform to the SampleProtocol.


Delegation works hand in hand with protocols because it allows a class to specify a delegate property which conforms to some protocol. Then a second class which actually conforms to that protocol can be assigned to that property. Now the first class can communicate with the second class through the delegate property using the methods and properties as defined by the protocol.

Declaring a delegate property


In Objective-C, declaring a delegate property involved using the “id” keyword as shown below.


In Swift, declaring a delegate property is just like declaring any other property and you specify the protocol name as the type of the property.

You may notice the question mark syntax which indicates that it’s a property with an optional value (there may or may not be an object assigned to it).

Calling a delegate method


In Objective-C, often times, you would see an If statement to check if there was an object assigned to the delegate property before calling the delegate method.


In Swift, you can take advantage of the question mark syntax. If the delegate property is empty, nothing after the question mark will be executed.


Protocols and delegation are widely used in Objective-C and they continue to be in Swift. It often takes beginners awhile to grasp the concept but once they do, it becomes a powerful tool in their arsenal.