何时以及如何在 Swift 中使用 @noreturn 属性? [英] When and how to use @noreturn attribute in Swift?

查看:25
本文介绍了何时以及如何在 Swift 中使用 @noreturn 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 guard-else 流的上下文中阅读了关键字 else 后用大括号括起来的代码块,必须调用标有 的函数noreturn 属性或使用 returnbreakcontinuethrow 转移控制.

I read the code block enclosed in curly braces after the keyword else in the context of a guard-else flow, must call a function marked with the noreturn attribute or transfer control using return, break, continue or throw.

最后一部分很清楚,而第一部分我不太明白.

The last part is quite clear, while I don't understand well the first.

首先,即使你没有声明任何返回类型,任何函数都会返回一些东西(至少是一个空元组).其次,我们什么时候可以使用 noreturn 函数?文档是否建议使用 noreturn 标记一些核心的内置方法?

First of all, any function returns something (an empty tuple at least) even if you don't declare any return type. Secondly, when can we use a noreturn function? Are the docs suggesting some core, built-in methods are marked with noreturn?

guard 语句的 else 子句是必需的,并且必须调用标有 noreturn 属性或传输程序的函数使用以下方法之一控制在 guard 语句的封闭范围之外以下陈述:

The else clause of a guard statement is required, and must either call a function marked with the noreturn attribute or transfer program control outside the guard statement’s enclosing scope using one of the following statements:

return

break

continue

throw

这是来源.

推荐答案

首先,即使你没有声明任何返回类型,任何函数都会返回一些东西(至少是一个空元组).

First of all, any function returns something (an empty tuple at least) even if you don't declare any return type.

(@noreturn 已过时;请参阅下面的 Swift 3 更新.)不,有些函数会立即终止进程并且不要返回给调用者.这些在 Swift 中被标记用@noreturn,比如

(@noreturn is obsolete; see Swift 3 Update below.) No, there are functions which terminate the process immediately and do not return to the caller. These are marked in Swift with @noreturn, such as

@noreturn public func fatalError(@autoclosure message: () -> String = default, file: StaticString = #file, line: UInt = #line)
@noreturn public func preconditionFailure(@autoclosure message: () -> String = default, file: StaticString = #file, line: UInt = #line)
@noreturn public func abort()
@noreturn public func exit(_: Int32)

可能还有更多.

(备注: 其他编程语言中也存在类似的注解或编译器,例如 C++11 中的 [[noreturn]]__attribute__((noreturn)) 作为 GCC 扩展,或 _Noreturn为了Clang 编译器.)

(Remark: Similar annotations exist in other programming languages or compilers, such as [[noreturn]] in C++11, __attribute__((noreturn)) as a GCC extension, or _Noreturn for the Clang compiler.)

如果它也终止,你可以用 @noreturn 标记你自己的函数过程无条件,例如通过调用其中一个内置函数,例如

You can mark your own function with @noreturn if it also terminates the process unconditionally, e.g. by calling one of the built-in functions, such as

@noreturn func myFatalError() {
    // Do something else and then ...
    fatalError("Something went wrong!")
}

现在您可以在 guard 语句的 else 子句中使用您的函数:

Now you can use your function in the else clause of a guard statement:

guard let n = Int("1234") else { myFatalError() }

@noreturn 函数也可用于标记不应该发生"并指示编程错误.一个简单的例子(摘自缺少返回 UITableViewCell):

@noreturn functions can also be used to mark cases that "should not occur" and indicate a programming error. A simple example (an extract from Missing return UITableViewCell):

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: MyTableViewCell

    switch (indexPath.row) {
    case 0:
        cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! MyTableViewCell
        cell.backgroundColor = UIColor.greenColor()
    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! MyTableViewCell
        cell.backgroundColor = UIColor.redColor()
    default:
        myFatalError()
    }
    // Setup other cell properties ...
    return cell
}

如果没有将 myFatalError() 标记为 @noreturn,编译器会在默认情况下抱怨缺少回报.

Without myFatalError() marked as @noreturn, the compiler would complain about a missing return in the default case.

更新:在 Swift 3 (Xcode 8 beta 6) 中的 @noreturn 属性已被 Never 返回类型替换,所以上面的例子现在将被写为

Update: In Swift 3 (Xcode 8 beta 6) the @noreturn attribute has been replaced by a Never return type, so the above example would now be written as

func myFatalError() -> Never  {
    // Do something else and then ...
    fatalError("Something went wrong!")
}

这篇关于何时以及如何在 Swift 中使用 @noreturn 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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