这个可能很简单。我们知道运算符&+对整数执行模运算(环绕),而运算符+则会导致错误。
$ swift
1> var x: Int8 = 100
x: Int8 = 100
2> x &+ x
$R0: Int8 = -56
3> x + x
Execution interrupted. Enter Swift code to recover and continue.这是什么错误?我不能抓住它,我也不能把它交给一个可选的:
4> do {try x + x} catch {print("got it")}
Execution interrupted. Enter Swift code to recover and continue.
5> try? x + x
Execution interrupted. Enter Swift code to recover and continue.我很确定这类错误是来自this Stack Overflow question的相同类型的错误(除以零),但是我不知道这种错误是否可以被捕获。我错过了什么简单的东西?它能被困住吗?如果是这样的话,是怎么做的?
发布于 2020-02-01 11:04:59
看起来这已经成为Swift 5,addingReportingOverflow(_:)中的一种非静态方法。
例如,
UInt8.max.addingReportingOverflow(1)将返回(partialValue: 0, overflow: true)参见关于 manual page的更多信息
当然,以&开头的普通算术运算符允许溢出而不返回溢出报告,
UInt8.max &+ 1将0作为UInt8返回
https://stackoverflow.com/questions/35973899
复制相似问题