简体   繁体   English

Swift对类型注释的定义

[英]Swift's definition of Type Annotation

In Swift Programming Language guide, it has this definition for Type Annotation: "A type annotation explicitly specifies the type of a variable or expression ." 在Swift编程语言指南中,它具有类型注释的定义:“类型注释显式指定了变量表达式的类型。”

We all know how to specify the type of a variable , but how exactly do you specify the type of an expression ? 我们都知道如何指定变量的类型,但是如何精确指定表达式的类型呢?

The Apple documentation says: "in 苹果文档中说:“在

let someTuple: (Double, Double) = (2.14159, 2.71828)

the expression someTuple is specified to have the tuple type..." (emphasis mine). Thus a let or var binding is an example using a type annotation on an expression . 表达式 someTuple被指定为具有元组类型...”(强调我的)。因此, letvar绑定是在表达式上使用类型注释的示例。

A type annotation for a variable is illustrated as specifying the type for a function parameter: 变量的类型注释说明为指定函数参数的类型:

func someFunction(a:Int) { /* ... */ }

You use a colon to tell the compiler what type your expression or variable has. 您使用冒号来告诉编译器表达式或变量的类型。

eg 例如

let year : Int = 2005 let isRaining : Bool = true

The : Int part is the type annotation here. : Int部分此处的类型注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM