iOSSwift中如何添加两个UInt8变量?
How two UInt8 variables are added in iOS Swift?
假设有两个变量:
let number1 : UInt8 = 100;
let number2 : UInt8 = 100;
您添加并打印它们
print(number1 + number2) //This prints 200
现在再定义一个
let number3 : UInt8 = 200;
现在尝试添加
print(number1 + number3) // Throws execution was interrupted
我知道 number1 和 number3 的总和将超出 UInt8 的范围,但显式转换也无济于事,例如,以下行也会给出相同的错误:
print(UInt8(number1 + number3)) // Throws execution was interrupted
我找到的方法是执行以下操作:
print(Int(number1) + Int(number3))
当 UInt8 的和超出范围时,是否有更好的方法来添加它们?
Girish K 古普塔,
UInt8 的最大范围为 0 到 255。您可以使用 UInt8.min
和 UInt8.max
进行检查。基本上是 0 到 2 的 8 次方。
print(number1 + number3) 的问题将 return 300。300 大于 255,因此崩溃。
当您添加两个 UInt8 结果时,默认情况下将转换为 UInt8,因此会发生崩溃
最后,当您 Int(number1) + Int(number3)
强制将 number1 和 number3 转换为 Int 时。
当您使用 Int 时,其值的范围取决于您所在的平台 运行 它是 32 位还是 64 位。例如,对于 32 位,它的范围可以是 -2,147,483,648 到 2,147,483,647。
当您将 Int 添加到 Int 时,结果将被类型转换为 Int。相信我 300 在范围内:)
根据你的问题有没有更好的方法:)
Apple 的文档明确指定并指示使用 Int 而不是 UInt8 或 UInt32 甚至 UInt64,除非使用 UInt8、UInt32 或 UInt64 是绝对必要的。
这里引用自 apple 的文档 :)
“Use UInt only when you specifically need an unsigned integer type
with the same size as the platform’s native word size. If this is not
the case, Int is preferred, even when the values to be stored are
known to be non-negative. A consistent use of Int for integer values
aids code interoperability, avoids the need to convert between
different number types, and matches integer type inference,”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).”
iBooks. https://itun.es/in/jEUH0.l
所以最好的办法是:) 按照苹果的说明:) 将 number1、number2 和 number3 更改为 Int :) 问题已解决:)
因此没有崩溃 :)
正如您所说,将两个 UInt8 变量都转换为 Int 会覆盖溢出时的默认异常,因为生成的 Int 现在有空间来容纳总和。
为了避免为每个操作强制转换变量,我们希望像这样重载运算符:
func + (left: UInt8, right: UInt8) -> Int {
return Int(left) + Int(right)
}
然而,这会给我们一个编译器错误,因为 + 运算符已经定义为添加两个 UInt8。
我们可以做的是 define a custom operator,说 ^+ 表示添加两个 UInt8,但将它们添加为 Int,如下所示:
infix operator ^+ { associativity left precedence 140 }
func ^+ (left: UInt8, right: UInt8) -> Int {
return Int(left) + Int(right)
}
然后我们可以在我们的算法中使用它:
print(number1 ^+ number3) // Prints 300
如果您希望结果溢出,您可以使用 the overflow operators from the standard library:
print(number1 &+ number3) // Prints 44
假设有两个变量:
let number1 : UInt8 = 100;
let number2 : UInt8 = 100;
您添加并打印它们
print(number1 + number2) //This prints 200
现在再定义一个
let number3 : UInt8 = 200;
现在尝试添加
print(number1 + number3) // Throws execution was interrupted
我知道 number1 和 number3 的总和将超出 UInt8 的范围,但显式转换也无济于事,例如,以下行也会给出相同的错误:
print(UInt8(number1 + number3)) // Throws execution was interrupted
我找到的方法是执行以下操作:
print(Int(number1) + Int(number3))
当 UInt8 的和超出范围时,是否有更好的方法来添加它们?
Girish K 古普塔,
UInt8 的最大范围为 0 到 255。您可以使用 UInt8.min
和 UInt8.max
进行检查。基本上是 0 到 2 的 8 次方。
print(number1 + number3) 的问题将 return 300。300 大于 255,因此崩溃。
当您添加两个 UInt8 结果时,默认情况下将转换为 UInt8,因此会发生崩溃
最后,当您 Int(number1) + Int(number3)
强制将 number1 和 number3 转换为 Int 时。
当您使用 Int 时,其值的范围取决于您所在的平台 运行 它是 32 位还是 64 位。例如,对于 32 位,它的范围可以是 -2,147,483,648 到 2,147,483,647。
当您将 Int 添加到 Int 时,结果将被类型转换为 Int。相信我 300 在范围内:)
根据你的问题有没有更好的方法:)
Apple 的文档明确指定并指示使用 Int 而不是 UInt8 或 UInt32 甚至 UInt64,除非使用 UInt8、UInt32 或 UInt64 是绝对必要的。
这里引用自 apple 的文档 :)
“Use UInt only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this is not the case, Int is preferred, even when the values to be stored are known to be non-negative. A consistent use of Int for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference,”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).” iBooks. https://itun.es/in/jEUH0.l
所以最好的办法是:) 按照苹果的说明:) 将 number1、number2 和 number3 更改为 Int :) 问题已解决:)
因此没有崩溃 :)
正如您所说,将两个 UInt8 变量都转换为 Int 会覆盖溢出时的默认异常,因为生成的 Int 现在有空间来容纳总和。
为了避免为每个操作强制转换变量,我们希望像这样重载运算符:
func + (left: UInt8, right: UInt8) -> Int {
return Int(left) + Int(right)
}
然而,这会给我们一个编译器错误,因为 + 运算符已经定义为添加两个 UInt8。
我们可以做的是 define a custom operator,说 ^+ 表示添加两个 UInt8,但将它们添加为 Int,如下所示:
infix operator ^+ { associativity left precedence 140 }
func ^+ (left: UInt8, right: UInt8) -> Int {
return Int(left) + Int(right)
}
然后我们可以在我们的算法中使用它:
print(number1 ^+ number3) // Prints 300
如果您希望结果溢出,您可以使用 the overflow operators from the standard library:
print(number1 &+ number3) // Prints 44