【问题标题】:Ambiguous reference to member on extension扩展时对成员的模糊引用
【发布时间】:2017-09-24 20:05:24
【问题描述】:

我使用 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", connection: nil)

我不知道如何解决它。我试过welf.session,因为我认为它超出了范围。

是什么导致了这个问题,我该如何解决?

【问题讨论】:

  • 1.将诊断添加到 Q,而不是诊断的描述。
  • 没错,我已经编辑了我的帖子。
  • @da1lbi3 使用示例时,只包含相关代码..不需要代码转储..它使您的问题更难阅读

标签: swift tokbox


【解决方案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)
}

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2019-11-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多