【问题标题】:Unexpected non-void return value in void function (Swift 3) [duplicate]void函数(Swift 3)中意外的非void返回值[重复]
【发布时间】:2023-03-09 17:24:02
【问题描述】:
func postUser(username: String, pass: String) -> Bool {
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        return true
                    case .failure(let error):
                        print(error)
                        return false
                }
            }
        }

【问题讨论】:

  • 你在一个闭包里面,它的返回类型是 Void,所以你不能从里面返回 Bool
  • 啊!!我错过了!感谢那! :)

标签: swift


【解决方案1】:

如果您正在调用像Alamofire.request 这样的异步方法,那么您需要在此异步方法结束时通知closures

试试这个

func postUser(username: String, pass: String, finishedClosured:@escaping ((Bool)->Void))  {
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        finishedClosured(true)
                    case .failure(let error):
                        print(error)
                        finishedClosured(false)
                }
            }
        }

使用它

 self.postUser(username: "your UserName", pass: "Your Pass") { (result) in
            if(result){
                debugPrint("All is fine")
            }else{
                debugPrint("All is wrong")
            }
        }

【讨论】:

  • 从那,调用该函数的语法是什么?新手在这里:(
  • @Creign 我的答案已更新为使用它部分
  • 谢谢! :)
【解决方案2】:

您不应该从此方法返回 true 或 false,因为这是异步网络调用,只需使用回调将数据获取到外部

func postUser(username: String, pass: String callback: @escaping (Bool) -> Void){
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        callback(true)  //using this you can send back the data                   

                   case .failure(let error):
                        print(error)
                        callback(false)

                }
            }
           //here you can return true or false but I dont think you should


        }

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多