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.

Protocols

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

Objective-C

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

Swift

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

Conforming to a protocol

Objective-C

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.

Swift

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

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

Objective-C

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

Swift

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

Objective-C

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.

Swift

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.

Conclusion

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.

9 Comments

  1. In Objective C, it is better to have your delegate declaration to conform NSObject Protocol , and have check with `- conformsToProtocol:` instead.

  2. Excellent information with very clear and detailed information about all concepts.
    I learned basics of swift within 2 days because of this information.

    Thanks a lot for this articles.

  3. Any idea how to use a Swift defined protocol in Objective-C? As you cannot include the Module-Swift.h to any .h file you need to use a forward declaration. That works with classes, but not with protocols. I simply get an error “Cannot find protocol definition for XX”. The protocol is marked with @objc.

    • Tapani, you’ve probably already figured it out, but to have an Objective-C class conform to a protocol declared in Swift in an Objective-C header file you need to forward
      declare the protocol like so:

      @protocol MyProtocolDeclaredInSwift;
      @interface SomeOCClass: NSObject {…

      Then import the Xcode generated header file, i.e. Something-Swift.h, as normal from with the Objective-C implementation file.

      • I think this has changed in newer Xcode versions. Earlier the forward declaration did not work. But thanks anyway.

Leave a Comment

Share
Tweet
Pin
Share
Buffer