0

I make use of the TokBox api. I want to use the signal method. But I get the Ambiguous reference to member 'session' error.

This is the code:

import UIKit
import OpenTok
import Firebase
import FontAwesome_swift

class CallView: UIViewController {

    private var session: OTSession?
    private var publisher  : CustomPublisher?

override func viewDidLoad() {
        super.viewDidLoad()

        //connect to the session
        connectToAnOpenTokSession()
    }

func connectToAnOpenTokSession() {
        session = OTSession(apiKey: "xxx", sessionId: "xxx", delegate: self)
        var error: OTError?
        session?.connect(withToken: "xxx", error: &error)
        if error != nil {
            print(error!)
        }
    }
}


extension CallView: OTSessionDelegate {
    func sessionDidConnect(_ session: OTSession) {
        print("The client connected to the OpenTok session.")

        var error: OTError? = nil
        defer {
            print(error.debugDescription)
        }
    }

 func chat(){
        var error: OTError? = nil

        try? session!.signal(withType: "chat", string: "bla", connection: nil)
        if error != nil {
            print("Signal error: \(error)")
        }
        else {
            print("Signal sent: ")
        }
    }

    func sessionDidDisconnect(_ session: OTSession) {
        print("The client disconnected from the OpenTok session.")
    }

    func session(_ session: OTSession, didFailWithError error: OTError) {
        print("The client failed to connect to the OpenTok session: \(error).")
    }
    func session(_ session: OTSession, streamCreated stream: OTStream) {
        print("A stream was created in the session.")
    }

    func session(_ session: OTSession, streamDestroyed stream: OTStream) {
        print("A stream was destroyed in the session.")
    }
}

extension CallView: OTPublisherDelegate {
    func publisher(_ publisher: OTPublisherKit, didFailWithError error: OTError) {
        print("The publisher failed: \(error)")
    }
}

I have the chat() method. but I get the Ambiguous reference to member error at this line:

try? session!.signal(withType: "chat", string: "bla", connection: nil)

I don't know how to fix it. I have tried welf.session because I thought it is out of scope.

What is causing this issue and how can I resolve it?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
da1lbi3
  • 4,369
  • 6
  • 31
  • 65

1 Answers1

1

Try appending the OTError instance to the function's parameters.

Also, avoid using force unwrapping and unhandled error catching.

guard let tokSession = session else {
    return
}

do {
    var error: OTError? = nil
    try tokSession.signal(withType: "chat", string: "bla", connection: nil, error: error)

    if error != nil {
        print("Signal error: \(error)")
    } else {
        print("Signal sent: ")
    }
} catch let error {
    print(error.localizedDescription)
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223