Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

ios - unexpected non-void return value in void function Swift 4

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.

Here is my code

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



                }
        }

    }

}

This part where i want to return

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...