Search code examples
iosobjective-cswiftopentok

Swift Protocol Conformance Error for OpenTok/TokBox


I attempted to translate the OpenTok getting started objective-c code into swift and have an error with protocol conformance, despite conforming to the protocol. It is only the existence of the OTPublisherDelegate protocol in my class definition that creates the scenario where the error occurs.

The error message is:

The type ViewController does not conform to the protocol 'OTPublisherKitDelegate'

But, at least to my knowledge, I have implemented all the methods from OTPublisherKitDelegate.

Here is the example code (with empty methods for brevity)

import UIKit
import OpenTok

class ViewController: UIViewController, OTSessionDelegate, OTSubscriberKitDelegate, OTPublisherDelegate {

//MARK:OTSessionDelegate implementation
func sessionDidConnect(session: OTSession!) { }
func sessionDidDisconnect(session: OTSession!) { }
func session(session: OTSession!, didFailWithError error: OTError!) { }
func session(session: OTSession!, streamCreated stream: OTStream!) { }
func session(session: OTSession!, streamDestroyed stream: OTStream!) { }

//MARK:OTSubscriberKidDelegate implementation
func subscriberDidConnectToStream(subscriber: OTSubscriberKit!) { }
func subscriber(subscriber: OTSubscriberKit!, didFailWithError error: OTError!) { }

//MARK:OTPublisherDelegate implementation
func publisher(publisher: OTPublisherKit!, streamCreated stream: OTStream!) { }
func publisher(publisher: OTPublisherKit!, streamDestroyed stream: OTStream!) { }
func publisher(publisher: OTPublisherKit!, didFailWithError: OTStream!) { }
}

if I removed OTPublisherDelegate from the list of inherited protocols the project compiles. Unfortunately I've been unable to arrive at a solution using the OpenTok documentation (and it's good documentation).

The OpenTok dependencies are managed via CocoaPods and is compiling for all other protocols within the OpenTok library.


Solution

  • Of course I figured it out AFTER posting.

    The issue was with this method signature

    func publisher(publisher: OTPublisherKit!, didFailWithError: OTError!) { }
    

    I forgot that Swift's method signatures must not only follow the same argument type and order but also the external AND internal names for the arguments. In my case I forgot the internal argument name on the second parameter.

    The correct method signature looks like this:

    func publisher(publisher: OTPublisherKit!, didFailWithError error: OTError!) { }
    

    Me -> Facepalm