无效函数Swift 4中意外的非无效返回值 [英] unexpected non-void return value in void function Swift 4

查看:33
本文介绍了无效函数Swift 4中意外的非无效返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 ECPLogin 中的 switch 语句返回响应正文,但它给了我这个错误在 void 中出现意外的非空返回值",即使我在第 33 行声明了函数 gotourl() -> String.请指教.

Iam trying to return the response body from the switch statment in ECPLogin but it give me this error "unexpected non-void return value in void" even though i declare the function gotourl() -> String in line 33. Please advice.

这是我的代码

import UIKit
import SwiftECP
import XCGLogger
class ViewController: UIViewController {

    @IBOutlet var UsernameField: UITextField!
    @IBOutlet var passwordField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func _Login(_ sender: Any) {
         self.gotourl()
//        performSegue(withIdentifier: "gotowelcome", sender: self)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func gotourl() -> String{
        let username: String = UsernameField.text!
        let password: String = passwordField.text!
        let protectedURL = URL(
            string: "https://itsapps.odu.edu/auth/getInfo.p"
            )!
        let logger = XCGLogger()
        logger.setup(level: .debug)

        ECPLogin(
            protectedURL: protectedURL,
            username: username,
            password: password,
            logger: logger
            ).start { event in
                switch event {

                case let .value( body) :
                    // If the request was successful, the protected resource will
                    // be available in 'body'. Make sure to implement a mechanism to
                    // detect authorization timeouts.

                    print("Response body: \(body)")
                    return body


                    // The Shibboleth auth cookie is now stored in the sharedHTTPCookieStorage.
                    // Attach this cookie to subsequent requests to protected resources.
                    // You can access the cookie with the following code:
                    if let cookies = HTTPCookieStorage.shared.cookies {
                        let shibCookie = cookies.filter { (cookie: HTTPCookie) in
                            cookie.name.range(of: "shibsession") != nil
                            }[0]
                        print(shibCookie)
                    }

                case let .failed(error):
                    // This is an AnyError that wraps the error thrown.
                    // This can help diagnose problems with your SP, your IdP, or even this library :)

                    switch error.cause {
                    case let ecpError as ECPError:
                        // Error with ECP
                        // User-friendly error message
                        print(ecpError.userMessage)

                        // Technical/debug error message
                        print(ecpError.description)
                    case let alamofireRACError as AlamofireRACError:
                        // Error with the networking layer
                        print(alamofireRACError.description)
                    default:
                        print("Unknown error!")
                        print(error)

                    }

                default:
                    break



                }
        }

    }

}

我想返回的部分

case let .value( body) :
                    // If the request was successful, the protected resource will
                    // be available in 'body'. Make sure to implement a mechanism to
                    // detect authorization timeouts.

                    print("Response body: \(body)")
                    return body

那么有办法解决吗?谢谢

So is there a way around it ? thanks

推荐答案

return 不是从 gotourl() 返回的.它从您传递给 ECPLogin 的闭包中返回.从您的代码和错误消息来看,您调用的方法似乎将完成闭包作为其最后一个参数,并且该闭包不应返回值(即,它返回 Void).这就是为什么你会得到错误——当这个闭包应该什么都不返回时,你正在返回一个字符串.看起来您使用的 ECPLogin 函数是异步的,这意味着它不会立即产生结果,而是会做一些工作并在完成后调用闭包.

That return isn't returning from gotourl(). It's returning from the closure you passed to ECPLogin. From your code and the error message it appears that the method you call takes a completion closure as its last argument, and this closure is not expected to return a value (that is, it returns Void). That's why you get the error-- you're returning a string when this closure is supposed to return nothing. It looks like the ECPLogin function you're using is asynchronous, meaning it doesn't produce a result immediately but instead does some work and calls the closure when it's done.

您的代码没有从 gotourl() 返回任何内容,这将是另一个与此相关的问题.

Your code doesn't return anything from gotourl(), which will be another problem, related to this.

如何修复它取决于您的应用需要如何工作.一些选项包括:

How to fix it depends on how your app needs to work. Some options include:

  • 更改 gotourl() 使其接受完成闭包而不是返回值.然后将您的 return 替换为调用此闭包的 lint.
  • 使用类似调度组的方式让您的代码等待 ECPLogin 调用完成,然后以这种方式返回一个值.
  • 使用其他一些选项,这样您就无需等待字符串可用,例如发布通知.
  • Change gotourl() so that it takes a completion closure instead of returning a value. Then replace your return with a lint that calls this closure.
  • Use something like a dispatch group to make your code wait until the ECPLogin call completes, and return a value that way.
  • Use some other option so that you don't need to wait until the string is available, like posting a notification.

这篇关于无效函数Swift 4中意外的非无效返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆