2017-09-24 66 views
0

我使用TokBox api。我想使用信號方法。但是我得到了成員'會話'錯誤的歧義引用。對擴展名成員的歧義引用

這是代碼:

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)") 
    } 
} 

我有chat()方法。但是我得到了歧義參考成員錯誤在這一行:

試試嗎? session!.signal(withType:「chat」,string:「bla」,連接:無)

我不知道如何解決它。我試過welf.session,因爲我認爲它超出了範圍。

是什麼導致了這個問題,我該如何解決它?

+0

1.將診斷添加到Q,而不是診斷描述。 –

+0

沒錯,我編輯了我的帖子。 – da1lbi3

回答

1

嘗試追加OTError實例到函數的參數。

此外,避免使用強力解開和未處理的錯誤捕捉。

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) 
} 
相關問題