Swift +错误处理:尽管满足所有条件,但Xcode抱怨try语句

时间:2019-05-31 20:12:11

标签: swift exception

我正在尝试通过建立websocket连接来创建try-catch-statement来捕获错误:

@IBAction func ConnectClick(_ sender: UIButton) {
        Connection = WebSocketConnection.sharedConnection
        let accessRequest = NSString(format: WS_CONNECTION_STRING as NSString, txtUser.text!, txtPassword.text!, "1111-2222-3333-4444", "1.0", "112233445566")
        Connection?.Client?.user = EncryptString(ATextToEncrypt: accessRequest, APassword: DEFAULT_ENCRYPTION_KEY as NSString)

        if !(Connection?.Client?.connected)! {
            do {
                try Connection?.Client?.connect(WS_CONNECTION_IP_PORT)
            } catch {
                print("Error... \(error)")
            }
        }
    }

这是connect函数的外观:

  public func connect(URL:String) throws -> Void {
    m_obj.connect(URL)
    if (m_obj.lastErrorCode() != 0) { throw IPWorksWSWebSocketClientError.Error(m_obj.lastErrorCode(), m_obj.lastError()) }
  }

但是,尽管如此,我还是得到了一个Swift编译器警告,所调用的函数不符合try语句的条件:

在“ try”表达式中没有对throw函数的调用。

我在做什么错了?

0 个答案:

没有答案