2017-09-24 8 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()メソッドを持っています。私はこの行のメンバエラーへのあいまいな参照を得ます:

try?セッション!。シグナル(withType: "チャット"、文字列: "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) 
} 
関連する問題