与 swift 中的 `return` 功能混淆 [英] confused with the functionality of `return` in swift

查看:53
本文介绍了与 swift 中的 `return` 功能混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Swift 中的 return 感到困惑.我知道它用于返回函数中的值,如果这样使用:

I am confused with return in Swift. I understand it's used to return the value in a function, if used like this:

func double(value: int) -> Int { 
    return value * 2
}

但我经常只看到使用了 return,就像在这样的可选绑定中的 guard 语句中一样:

But I often just see return being used, like in a guard statement in an optional binding like this:

guard let value = value else (
    print ("nothing")
    return
}

那么像这样在 guard 语句中只包含 return 的目的是什么?实际上,我经常在解包可选值时不仅在 guard 语句中看到这一点.在编写代码时,当我想使用字典中的可选字符串时,我总是会发现这个问题.

So what is the purpose of having just return in the guard statement like this? Actually, I often see this not only in guard statements when unwrapping optional values. I always find this problem when writing code, when I want to use an optional string from a dictionary.

let info = ["name": "sarah", "hometown": "sydney"]

class UserInfo {

    func getTheName() -> String {
        guard let name = info["name"] else { return }

        return name
    }
}
// Compile time error: "Non-void function should return a value"

即使我写了return name,我也收到此错误.Xcode 仍然抱怨我没有返回值.是不是因为guard语句中的return?

I get this error even though I have written return name. Xcode still complains that I have not returned a value. Is it because of the return in the guard statement?

那么,你能告诉我 return 在 Swift 中的用途吗?这让我很困惑.

So, could you please tell me the purpose of return in Swift? It is confusing for me.

推荐答案

return 不带任何参数返回 Void.这种形式的 return 语句只能用于返回 Void 的函数.

return without any argument returns Void. This form of the return statement can only be used with a function that returns Void.

一旦 return 语句执行,函数就会退出并且函数中不再有代码执行.由于您在 guard 语句中有一个 return,第二个 return name 不会被执行(它无论如何也不会,因为它不会没有要返回的名称),这就是为什么会出现编译器错误;编译器查看您的函数可以用来返回某些内容的所有路径,并确保所有这些路径返回函数签名所说的内容.

Once the return statement executes, the function exits and no more code in your function executes. Since you have a return in the guard statement, the second return name won't be executed (it couldn't anyway since it wouldn't have a name to return), which is why you get a compiler error; the compiler looks at all of the paths that your function could take to return something and ensures that all of those paths return what the function signature says it will.

您问题中的函数声明它返回一个 String,因此您不能简单地在 guard 语句中说 return返回 Void,违反了你的函数签名所表达的契约.

The function in your question states that it returns a String, so you can't simply say return in the guard statement as that returns Void, violating the contract expressed by your function signature.

您可以返回一个不是 Void 的默认值:

You could return a default value that isn't Void:

func getTheName () -> String {
    guard let name = info["name"] else {
        return ""
    }
    return name
}    

使用 nil-coalescing 运算符可以更简洁地编写;返回信息["name"] ??""

This could be written much more succinctly using the nil-coalescing operator; return info["name"] ?? ""

您还可以在返回 Void(或没有显式返回类型,在这种情况下隐式理解为返回 Void<)的函数中使用 return/code>)

You can also use return in a function that returns Void (or has no explicit return type, in which case it is implicitly understood to return Void)

所以你可以有一个类似的功能:

So you could have a function like:

func maybePrint(theMessage: String?) -> Void {
    guard let msg = theMessage else {
        return
    }
    print(msg)
}

这篇关于与 swift 中的 `return` 功能混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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