且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无效函数Swift 4中意外的非无效返回值

更新时间:2022-11-07 23:38:01

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.