数的Swift算术和比较运算符重载列表? [英] list of Swift arithmetic and comparison operator overloads for numbers?

查看:66
本文介绍了数的Swift算术和比较运算符重载列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中是否有地方或可以在Xcode 6中动态查找的内容,可以显示所有已定义的数字重载运算符,例如二进制算术运算符和比较运算符?

Is there a place in the documentation or something I can look up dynamically in Xcode 6 that shows all the defined operator overloads for numbers such as the binary arithmetic and comparison operators?

Swift支持所有数字类型的四个标准算术运算符:
加法(+)
减法(-)
乘法(*)
除法(/)

Swift supports the four standard arithmetic operators for all number types:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)

Swift支持所有标准C比较运算符:
等于(a == b)
不等于(a!= b)
大于(a> b)
小于(a< b)
大于或等于(a> = b)
小于或等于(a< = b)

Swift supports all standard C comparison operators:
Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)

我想知道的原因是,我可以看到何时必须使用类型转换,何时不使用,因为两种兼容类型都有内置的运算符重载.

The reason I would like to know is so that I can see when I'll have to use type casting and when I won't because there is a built-in operator overload for two compatible types.

我对类型转换和自动升序有一个相关的问题,但是在发布之前,我想确保我了解Swift默认定义的规则.

I have a related question on type casting and automatic upscaling, but before I posted it I wanted to make sure I understand the rules Swift defines by default.

推荐答案

正如Martin所说,您可以看到一种头文件,该文件通过命令单击Int或其他某些Swift类型来声明这些功能.例如,乘法函数如下所示:

As Martin said, you can see a sort of header file that declares these functions by command-clicking Int or some other Swift type. For example the multiplication functions look like this:

func *(lhs: UInt8, rhs: UInt8) -> UInt8
func *(lhs: Float, rhs: Float) -> Float
func *(lhs: Int, rhs: Int) -> Int
func *(lhs: UInt, rhs: UInt) -> UInt
func *(lhs: Int64, rhs: Int64) -> Int64
func *(lhs: Float80, rhs: Float80) -> Float80
func *(lhs: Double, rhs: Double) -> Double
func *(lhs: UInt64, rhs: UInt64) -> UInt64
func *(lhs: Int32, rhs: Int32) -> Int32
func *(lhs: UInt32, rhs: UInt32) -> UInt32
func *(lhs: Int16, rhs: Int16) -> Int16
func *(lhs: UInt16, rhs: UInt16) -> UInt16
func *(lhs: Int8, rhs: Int8) -> Int8

所有算术函数均采用两个相同类型的数字,并返回该类型的数字.这就是为什么您经常必须在算术运算之前进行转换.

All of the arithmetic functions take two numbers of the same type and return a number of that type. That's why you often have to do conversions before arithmetic operations.

您只需要强制转换已经具有类型的变量或常量.您可以对原始数字文字进行任何算术运算.

You only have to cast variables or constants that already have a type. You can do any arithmetic operation on raw numeric literals.

在这种情况下,两个操作数都是常量,在定义它们时,swift隐式地为其赋予了类型:

In this situation both operands are constants and swift gave them a type implicitly when they were defined:

let a = 42 //compiler assumes Int
let b = 3.14 //compiler assumes Float
a + Int(b) //returns Int (45)

但这并不意味着将小数点后的值强制将文字分配给Float变量.您可以显式强制输入类型:

but that doesn't mean having a decimal point will force the literal to be assigned to a Float variable. You can explicitly force the type:

var x: Int = 1.1 //shows 1
var y: Float = 1 //shows 1.0

这就是为什么您可以做这样的事情:

That's why you can do stuff like this:

var foo = 10
foo + 10.4

即使10.4是浮点文字,因为Swift已经隐式地将foo键入为Integer,因此它也将10.4文字也视为Integer并愉快地将它们加在一起.

even though 10.4 is a floating-point literal, because Swift already implicitly typed foo as an Integer it treats the 10.4 literal as an Integer as well and happily adds them together.

这篇关于数的Swift算术和比较运算符重载列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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