Swift返回后执行代码,难以置信

我创建了一个 tableview 并且在单元格中有一个切换,我单击切换将在单元格的协议中有一个函数,并且在该函数的实现中我有一个这样的逻辑:

执行返回后的打印功能!不敢相信有人知道为什么吗?或者这是苹果的bug?

回答

屏幕截图中的警告准确地告诉您发生了什么:

“return”后面的表达式被视为“return”的参数。

当 return 被调用时,它有时需要一个表达式,在这种情况下是一个函数。

如果您不希望 print 语句成为 return 的一部分,则可以在 return;末尾添加 a

  • In Swift, return statement in a Void function can return only one specific value Void().
    (An instance of Void, which is equivalent as the empty tuple ().)
    And the return type of print is Void, which means it returns Void().

    Thus, the result of calling print("what?") can be a return value of Void function as you see.

    Thanks answer from here:https://developer.apple.com/forums/thread/671904#671904021


以上是Swift返回后执行代码,难以置信的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>