Swift3.0官方文档与中文翻译对照

The Basics(基础部分上)

2017-01-14  本文已影响21人  金旭峰

  个人并没有把Swift 初见 , Swift 版本历史记录 等收录, 仅限个人爱好, 在此对Swift 社区再次表示感谢,是他们的辛苦付出才有了我们快速学习的可能性, 非常非常感谢社区!

The Basics

Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C.

Swift 是一门开发 iOS, macOS, watchOS 和 tvOS 应用的新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。

Swift provides its own versions of all fundamental C and Objective-C types, includingIntfor integers,DoubleandFloatfor floating-point values,Boolfor Boolean values, andStringfor textual data. Swift also provides powerful versions of the three primary collection types,Array,Set, andDictionary, as described inCollection Types.

Swift 包含了 C 和 Objective-C 上所有基础数据类型,Int表示整型值;Double和Float表示浮点型值;Bool是布尔型值;String是文本型数据。 Swift 还提供了三个基本的集合类型,Array,Set和Dictionary,详见集合类型

Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values cannot be changed. These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that do not need to change.

就像 C 语言一样,Swift 使用变量来进行存储并通过变量名来关联值。在 Swift 中,广泛的使用着值不可变的变量,它们就是常量,而且比 C语言的常量更强大。在 Swift 中,如果你要处理的值不需要改变,那使用常量可以让你的代码更加安全并且更清晰地表达你的意图。

In addition to familiar types, Swift introduces advanced types not found in Objective-C, such as tuples. Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

除了我们熟悉的类型,Swift 还增加了 Objective-C 中没有的高阶数据类型比如元组(Tuple)。元组可以让你创建或者传递一组数据,比如作为函数的返回值时,你可以用一个元组可以返回多个值。

Swift also introduces optional types, which handle the absence of a value. Optionals say either “thereisa value, and it equalsx” or “thereisn’ta value at all”. Using optionals is similar to usingnilwith pointers in Objective-C, but they work for any type, not just classes. Not only are optionals safer and more expressive thannilpointers in Objective-C, they are at the heart of many of Swift’s most powerful features.

Swift 还增加了可选(Optional)类型,用于处理值缺失的情况。可选表示 “那儿有一个值,并且它等于x” 或者 “那儿没有值” 。可选有点像在 Objective-C 中使用nil,但是它可以用在任何类型上,不仅仅是类。可选类型比 Objective-C 中的nil指针更加安全也更具表现力,它是 Swift 许多强大特性的重要组成部分。

Swift is atype-safelanguage, which means the language helps you to be clear about the types of values your code can work with. If part of your code expects aString, type safety prevents you from passing it anIntby mistake. Likewise, type safety prevents you from accidentally passing an optionalStringto a piece of code that expects a nonoptionalString. Type safety helps you catch and fix errors as early as possible in the development process.

Swift 是一门类型安全的语言,这意味着 Swift 可以让你清楚地知道值的类型。如果你的代码期望得到一个String,类型安全会阻止你不小心传入一个Int。同样的,如果你的代码期望得到一个String,类型安全会阻止你意外传入一个可选的String。类型安全可以帮助你在开发阶段尽早发现并修正错误。

Constants and Variables (常量和变量)

Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number10or the string"Hello"). The value of aconstantcannot be changed once it is set, whereas avariablecan be set to a different value in the future.

常量和变量把一个名字(比如maximumNumberOfLoginAttempts或者welcomeMessage)和一个指定类型的值(比如数字10或者字符串"Hello")关联起来。常量的值一旦设定就不能改变,而变量的值可以随意更改。

1. letmaximumNumberOfLoginAttempts=10

2. varcurrentLoginAttempt=0

This code can be read as:

这两行代码可以被理解为:

“Declare a new constant calledmaximumNumberOfLoginAttempts, and give it a value of10. Then, declare a new variable calledcurrentLoginAttempt, and give it an initial value of0.”

“声明一个名字是maximumNumberOfLoginAttempts的新常量,并给它一个值10。然后,声明一个名字是currentLoginAttempt的变量并将它的值初始化为0。”

In this example, the maximum number of allowed login attempts is declared as a constant, because the maximum value never changes. The current login attempt counter is declared as a variable, because this value must be incremented after each failed login attempt.

在这个例子中,允许的最大尝试登录次数被声明为一个常量,因为这个值不会改变。当前尝试登录次数被声明为一个变量,因为每次尝试登录失败的时候都需要增加这个值。

You can declare multiple constants or multiple variables on a single line, separated by commas:

你可以在一行中声明多个常量或者多个变量,用逗号隔开:

var x=0.0,y=0.0,z=0.0

Note (注意)

If a stored value in your code is not going to change, always declare it as a constant with theletkeyword. Use variables only for storing values that need to be able to change.

如果你的代码中有不需要改变的值,请使用let关键字将它声明为常量。只将需要改变的值声明为变量。

Type Annotations (类型标注)

You can provide atype annotationwhen you declare a constant or variable, to be clear about the kind of values the constant or variable can store. Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.

当你声明常量或者变量的时候可以加上类型标注(type annotation),说明常量或者变量中要存储的值的类型。如果要添加类型标注,需要在常量或者变量名后面加上一个冒号和空格,然后加上类型名称。

This example provides a type annotation for a variable calledwelcomeMessage, to indicate that the variable can storeStringvalues:

这个例子给welcomeMessage变量添加了类型标注,表示这个变量可以存储String类型的值:

varwelcomeMessage:String

The colon in the declaration means“…of type…,”so the code above can be read as:

声明中的冒号代表着“是...类型”,所以这行代码可以被理解为:

“Declare a variable calledwelcomeMessagethat is of typeString.”

“声明一个类型为String,名字为welcomeMessage的变量。”

The phrase “of typeString” means “can store anyStringvalue.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored.

“类型为String”的意思是“可以存储任意String类型的值。”

ThewelcomeMessagevariable can now be set to any string value without error:

welcomeMessage变量现在可以被设置成任意字符串:

welcomeMessage="Hello"

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

你可以在一行中定义多个同样类型的变量,用逗号分割,并在最后一个变量名之后添加类型标注:

var red,green,blue:Double

Note (注意)

It is rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it is defined, Swift can almost always infer the type to be used for that constant or variable, as described inType Safety and Type Inference. In thewelcomeMessageexample above, no initial value is provided, and so the type of thewelcomeMessagevariable is specified with a type annotation rather than being inferred from an initial value.

一般来说你很少需要写类型标注。如果你在声明常量或者变量的时候赋了一个初始值,Swift可以推断出这个常量或者变量的类型,请参考类型安全和类型推断。在上面的例子中,没有给welcomeMessage赋初始值,所以变量welcomeMessage的类型是通过一个类型标注指定的,而不是通过初始值推断的。

Naming Constants and Variables (常量和变量的命名)

Constant and variable names can contain almost any character, including Unicode characters:

你可以用任何你喜欢的字符作为常量和变量名,包括 Unicode 字符:

1. let π=3.14159

2. let 你好="你好世界"

3. let 🐶🐮="dogcow"

Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.

常量与变量名不能包含数学符号,箭头,保留的(或者非法的)Unicode 码位,连线与制表符。也不能以数字开头,但是可以在常量与变量名的其他地方包含数字。

Once you’ve declared a constant or variable of a certain type, you can’t redeclare it again with the same name, or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant.

一旦你将常量或者变量声明为确定的类型,你就不能使用相同的名字再次进行声明,或者改变其存储的值的类型。同时,你也不能将常量与变量进行互转。

Note (注意)

If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with backticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

如果你需要使用与Swift保留关键字相同的名称作为常量或者变量名,你可以使用反引号(`)将关键字包围的方式将其作为名字使用。无论如何,你应当避免使用关键字作为常量或变量名,除非你别无选择。

You can change the value of an existing variable to another value of a compatible type. In this example, the value offriendlyWelcomeis changed from"Hello!"to"Bonjour!":

你可以更改现有的变量值为其他同类型的值,在下面的例子中,friendlyWelcome的值从"Hello!"改为了"Bonjour!":

1. var friendlyWelcome="Hello!"

2. friendlyWelcome="Bonjour!"

// friendlyWelcome is now "Bonjour!"

Unlike a variable, the value of a constant cannot be changed once it is set. Attempting to do so is reported as an error when your code is compiled:

与变量不同,常量的值一旦被确定就不能更改了。尝试这样做会导致编译时报错:

1. let languageName="Swift"

2. languageName="Swift++"

// This is a compile-time error: languageName cannot be changed.

Printing Constants and Variables (输出常量和变量)

You can print the current value of a constant or variable with theprint(_:separator:terminator:)function:

你可以用print(_:separator:terminator:)函数来输出当前常量或变量的值:

1. print(friendlyWelcome)

// Prints "Bonjour!"

Theprint(_:separator:terminator:)function is a global function that prints one or more values to an appropriate output. In Xcode, for example, theprint(_:separator:terminator:)function prints its output in Xcode’s “console” pane. Theseparatorandterminatorparameter have default values, so you can omit them when you call this function. By default, the function terminates the line it prints by adding a line break. To print a value without a line break after it, pass an empty string as the terminator—for example,print(someValue, terminator: ""). For information about parameters with default values, seeDefault Parameter Values.

print(_:separator:terminator:)是一个用来输出一个或多个值到适当输出区的全局函数。如果你用 Xcode,print(_:separator:terminator:)将会输出内容到“console”面板上。separator和terminator参数具有默认值,因此你调用这个函数的时候可以忽略它们。默认情况下,该函数通过添加换行符来结束当前行。如果不想换行,可以传递一个空字符串给terminator参数--例如,print(someValue, terminator:"")。关于参数默认值的更多信息,请参考默认参数值

Swift usesstring interpolationto include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable. Wrap the name in parentheses and escape it with a backslash before the opening parenthesis:

Swift 用字符串插值(string interpolation)的方式把常量名或者变量名当做占位符加入到长字符串中,Swift 会用当前常量或变量的值替换这些占位符。将常量或变量名放入圆括号中,并在开括号前使用反斜杠将其转义:

1. print("The current value of friendlyWelcome is\(friendlyWelcome)")

// Prints "The current value of friendlyWelcome is Bonjour!"

Note (注意)

All options you can use with string interpolation are described inString Interpolation.

字符串插值所有可用的选项,请参考字符串插值

Comments (注释)

Use comments to include nonexecutable text in your code, as a note or reminder to yourself. Comments are ignored by the Swift compiler when your code is compiled.

请将你的代码中的非执行文本注释成提示或者笔记以方便你将来阅读。Swift 的编译器将会在编译代码时自动忽略掉注释部分。

Comments in Swift are very similar to comments in C. Single-line comments begin with two forward-slashes (//):

Swift 中的注释与 C 语言的注释非常相似。单行注释以双正斜杠(//)作为起始标记:

// This is a comment.

Multiline comments start with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/):

你也可以进行多行注释,其起始标记为单个正斜杠后跟随一个星号(/*),终止标记为一个星号后跟随单个正斜杠(*/):

/* This is also a comment

but is written over multiple lines. */

Unlike multiline comments in C, multiline comments in Swift can be nested inside other multiline comments. You write nested comments by starting a multiline comment block and then starting a second multiline comment within the first block. The second block is then closed, followed by the first block:

与 C 语言多行注释不同,Swift 的多行注释可以嵌套在其它的多行注释之中。你可以先生成一个多行注释块,然后在这个注释块之中再嵌套成第二个多行注释。终止注释时先插入第二个注释块的终止标记,然后再插入第一个注释块的终止标记:

/* This is the start of the first multiline comment.

/* This is the second, nested multiline comment. */

This is the end of the first multiline comment. */

Nested multiline comments enable you to comment out large blocks of code quickly and easily, even if the code already contains multiline comments.

通过运用嵌套多行注释,你可以快速方便的注释掉一大段代码,即使这段代码之中已经含有了多行注释块。

Semicolons (分号)

Unlike many other languages, Swift does not require you to write a semicolon (;) after each statement in your code, although you can do so if you wish. However, semicolonsarerequired if you want to write multiple separate statements on a single line:

与其他大部分编程语言不同,Swift 并不强制要求你在每条语句的结尾处使用分号(;),当然,你也可以按照你自己的习惯添加分号。有一种情况下必须要用分号,即你打算在同一行内写多条独立的语句:

let cat="🐱";print(cat)

// Prints "🐱"

Integers (整数)

Integersare whole numbers with no fractional component, such as42and-23. Integers are eithersigned(positive, zero, or negative) orunsigned(positive or zero).

整数就是没有小数部分的数字,比如42和-23。整数可以是有符号(正、负、零)或者无符号(正、零)。

Swift provides signed and unsigned integers in 8, 16, 32, and 64 bit forms. These integers follow a naming convention similar to C, in that an 8-bit unsigned integer is of typeUInt8, and a 32-bit signed integer is of typeInt32. Like all types in Swift, these integer types have capitalized names.

Swift 提供了8,16,32和64位的有符号和无符号整数类型。这些整数类型和 C 语言的命名方式很像,比如8位无符号整数类型是UInt8,32位有符号整数类型是Int32。就像 Swift 的其他类型一样,整数类型采用大写命名法。

Integer Bounds (整数范围)

You can access the minimum and maximum values of each integer type with itsminandmaxproperties:

你可以访问不同整数类型的min和max属性来获取对应类型的最小值和最大值:

1. let minValue=UInt8.min  // minValue is equal to 0, and is of type UInt8

2. let maxValue=UInt8.max  // maxValue is equal to 255, and is of type UInt8

The values of these properties are of the appropriate-sized number type (such asUInt8in the example above) and can therefore be used in expressions alongside other values of the same type.

min和max所传回值的类型,正是其所对的整数类型(如上例UInt8, 所传回的类型是UInt8),可用在表达式中相同类型值旁。

Int

In most cases, you don’t need to pick a specific size of integer to use in your code. Swift provides an additional integer type,Int, which has the same size as the current platform’s native word size:

一般来说,你不需要专门指定整数的长度。Swift 提供了一个特殊的整数类型Int,长度与当前平台的原生字长相同:

On a 32-bit platform,Intis the same size asInt32.

在32位平台上,Int和Int32长度相同。

On a 64-bit platform,Intis the same size asInt64.

在64位平台上,Int和Int64长度相同。

Unless you need to work with a specific size of integer, always useIntfor integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms,Intcan store any value between-2,147,483,648and2,147,483,647, and is large enough for many integer ranges.

除非你需要特定长度的整数,一般来说使用Int就够了。这可以提高代码一致性和可复用性。即使是在32位平台上,Int可以存储的整数范围也可以达到-2,147,483,648~2,147,483,647,大多数时候这已经足够大了。

UInt

Swift also provides an unsigned integer type,UInt, which has the same size as the current platform’s native word size:

Swift 也提供了一个特殊的无符号类型UInt,长度与当前平台的原生字长相同:

On a 32-bit platform,UIntis the same size asUInt32.

在32位平台上,UInt和UInt32长度相同。

On a 64-bit platform,UIntis the same size asUInt64.

在64位平台上,UInt和UInt64长度相同。

Note (注意)

UseUIntonly 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,Intis preferred, even when the values to be stored are known to be non-negative. A consistent use ofIntfor integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described inType Safety and Type Inference.

尽量不要使用UInt,除非你真的需要存储一个和当前平台原生字长相同的无符号整数。除了这种情况,最好使用Int,即使你要存储的值已知是非负的。统一使用Int可以提高代码的可复用性,避免不同类型数字之间的转换,并且匹配数字的类型推断,请参考类型安全和类型推断

Floating-Point Numbers (浮点数)

Floating-point numbersare numbers with a fractional component, such as3.14159,0.1, and-273.15.

浮点数是有小数部分的数字,比如3.14159,0.1和-273.15。

Floating-point types can represent a much wider range of values than integer types, and can store numbers that are much larger or smaller than can be stored in anInt. Swift provides two signed floating-point number types:

浮点类型比整数类型表示的范围更大,可以存储比Int类型更大或者更小的数字。Swift 提供了两种有符号浮点数类型:

Double represents a 64-bit floating-point number.

Double表示64位浮点数。当你需要存储很大或者很高精度的浮点数时请使用此类型。

Float represents a 32-bit floating-point number.

Float表示32位浮点数。精度要求不高的话可以使用此类型。


Note (注意)

Doublehas a precision of at least 15 decimal digits, whereas the precision ofFloatcan be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate,Doubleis preferred.

Double精确度很高,至少有15位数字,而Float只有6位数字。选择哪个类型取决于你的代码需要处理的值的范围,在两种类型都匹配的情况下,将优先选择Double。

Type Safety and Type Inference (类型安全和类型判断)

Swift is atype-safelanguage. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects aString, you can’t pass it anIntby mistake.

Swift 是一个类型安全(type safe)的语言。类型安全的语言可以让你清楚地知道代码要处理的值的类型。如果你的代码需要一个String,你绝对不可能不小心传进去一个Int。

Because Swift is type safe, it performstype checkswhen compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

由于 Swift 是类型安全的,所以它会在编译你的代码时进行类型检查(type checks),并把不匹配的类型标记为错误。这可以让你在开发的时候尽早发现并修复错误。

Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant and variable that you declare. If you don’t specify the type of value you need, Swift usestype inferenceto work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

当你要处理不同类型的值时,类型检查可以帮你避免错误。然而,这并不是说你每次声明常量和变量的时候都需要显式指定类型。如果你没有显式指定类型,Swift 会使用类型推断(type inference)来选择合适的类型。有了类型推断,编译器可以在编译代码的时候自动推断出表达式的类型。原理很简单,只要检查你赋的值即可。

Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C. Constants and variables are still explicitly typed, but much of the work of specifying their type is done for you.

因为有类型推断,和 C 或者 Objective-C 比起来 Swift 很少需要声明类型。常量和变量虽然需要明确类型,但是大部分工作并不需要你自己来完成。

Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning aliteral value(orliteral) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as42and3.14159in the examples below.)

当你声明常量或者变量并赋初值的时候类型推断非常有用。当你在声明常量或者变量的时候赋给它们一个字面量(literal value 或 literal)即可触发类型推断。(字面量就是会直接出现在你代码中的值,比如42和3.14159。)

For example, if you assign a literal value of42to a new constant without saying what type it is, Swift infers that you want the constant to be anInt, because you have initialized it with a number that looks like an integer:

例如,如果你给一个新常量赋值42并且没有标明类型,Swift 可以推断出常量类型是Int,因为你给它赋的初始值看起来像一个整数:

1. let meaningOfLife=42

// meaningOfLife is inferred to be of type Int

// meaningOfLife 会被推测为 Int 类型

Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create aDouble:

同理,如果你没有给浮点字面量标明类型,Swift 会推断你想要的是Double:

1. let pi=3.14159

// pi is inferred to be of type Double

// pi 会被推测为 Double 类型

Swift always choosesDouble(rather thanFloat) when inferring the type of floating-point numbers.

当推断浮点数的类型时,Swift 总是会选择Double而不是Float。

If you combine integer and floating-point literals in an expression, a type ofDoublewill be inferred from the context:

如果表达式中同时出现了整数和浮点数,会被推断为Double类型:

1. let anotherPi=3+0.14159

// anotherPi is also inferred to be of type Double

// anotherPi 会被推测为 Double 类型

The literal value of3 has no explicit type in and of itself, and so an appropriate output type of Doubleis inferred from the presence of a floating-point literal as part of the addition.

原始值3没有显式声明类型,而表达式中出现了一个浮点字面量,所以表达式会被推断为Double类型。

Numeric Literals

Integer literals can be written as:

整数字面量可以被写作:

A decimalnumber, with no prefix

一个十进制数,没有前缀

A binarynumber, with a0bprefix

一个二进制数,前缀是0b

A noctalnumber, with a0oprefix

一个八进制数,前缀是0o

A hexadecimalnumber, with a0xprefix

一个十六进制数,前缀是0x


All of these integer literals have a decimal value of17:

下面的所有整数字面量的十进制值都是17:

let decimalInteger = 17

let binaryInteger = 0b10001      // 二进制的17

let octalInteger = 0o21          // 八进制的17

let hexadecimalInteger = 0x11    // 十六进制的17

Floating-point literals can be decimal (with no prefix), or hexadecimal (with a0xprefix). They must always have a number (or hexadecimal number) on both sides of the decimal point. Decimal floats can also have an optionalexponent, indicated by an uppercase or lowercasee; hexadecimal floats must have an exponent, indicated by an uppercase or lowercase p.

浮点字面量可以是十进制(没有前缀)或者是十六进制(前缀是0x)。小数点两边必须有至少一个十进制数字(或者是十六进制的数字)。十进制浮点数也可以有一个可选的指数(exponent),通过大写或者小写的e来指定;十六进制浮点数必须有一个指数,通过大写或者小写的p来指定。

For decimal numbers with an exponent ofexp, the base number is multiplied by 10exp:

如果一个十进制数的指数为exp,那这个数相当于基数和10^exp的乘积:

1.25e2 means 1.25 x 10^2, or 125.0.

1.25e-2 means 1.25 x 10^-2, or 0.0125.

For hexadecimal numbers with an exponent ofexp, the base number is multiplied by 2exp:

如果一个十六进制数的指数为exp,那这个数相当于基数和2^exp的乘积:

0xFp2 means 15 x 2^2, or 60.0.

0xFp-2 means 15 x 2^-2, or 3.75.

All of these floating-point literals have a decimal value of 12.1875:

下面的这些浮点字面量都等于十进制的12.1875:

let decimalDouble=12.1875

let exponentDouble=1.21875e1

let hexadecimalDouble=0xC.3p0

Numeric literals can contain extra formatting to make them easier to read. Both integers and floats can be padded with extra zeros and can contain underscores to help with readability. Neither type of formatting affects the underlying value of the literal:

数值类字面量可以包括额外的格式来增强可读性。整数和浮点数都可以添加额外的零并且包含下划线,并不会影响字面量:

let paddedDouble=000123.456

let oneMillion=1_000_000

let justOverOneMillion=1_000_000.000_000_1

Numeric Type Conversion (数值型类型转换)

Use theInttype for all general-purpose integer constants and variables in your code, even if they are known to be non-negative. Using the default integer type in everyday situations means that integer constants and variables are immediately interoperable in your code and will match the inferred type for integer literal values.

通常来讲,即使代码中的整数常量和变量已知非负,也请使用Int类型。总是使用默认的整数类型可以保证你的整数常量和变量可以直接被复用并且可以匹配整数类字面量的类型推断。

Use other integer types only when they are specifically needed for the task at hand, because of explicitly-sized data from an external source, or for performance, memory usage, or other necessary optimization. Using explicitly-sized types in these situations helps to catch any accidental value overflows and implicitly documents the nature of the data being used.

只有在必要的时候才使用其他整数类型,比如要处理外部的长度明确的数据或者为了优化性能、内存占用等等。使用显式指定长度的类型可以及时发现值溢出并且可以暗示正在处理特殊数据。

Integer Conversion (整数转换)

The range of numbers that can be stored in an integer constant or variable is different for each numeric type. AnInt8constant or variable can store numbers between-128and127, whereas aUInt8constant or variable can store numbers between0and255. A number that will not fit into a constant or variable of a sized integer type is reported as an error when your code is compiled:

不同整数类型的变量和常量可以存储不同范围的数字。Int8类型的常量或者变量可以存储的数字范围是-128~127,而UInt8类型的常量或者变量能存储的数字范围是0~255。如果数字超出了常量或者变量可存储的范围,编译的时候会报错:

let cannotBeNegative:UInt8=-1

// UInt8 cannot store negative numbers, and so this will report an error

// UInt8 类型不能存储负数,所以会报错

let tooBig:Int8=Int8.max+1

// Int8 cannot store a number larger than its maximum value,  and so this will also report an error

// Int8 类型不能存储超过最大值的数,所以会报错

Because each numeric type can store a different range of values, you must opt in to numeric type conversion on a case-by-case basis. This opt-in approach prevents hidden conversion errors and helps make type conversion intentions explicit in your code.

由于每种整数类型都可以存储不同范围的值,所以你必须根据不同情况选择性使用数值型类型转换。这种选择性使用的方式,可以预防隐式转换的错误并让你的代码中的类型转换意图变得清晰。

To convert one specific number type to another, you initialize a new number of the desired type with the existing value. In the example below, the constanttwoThousandis of typeUInt16, whereas the constantoneis of typeUInt8. They cannot be added together directly, because they are not of the same type. Instead, this example callsUInt16(one)to create a newUInt16initialized with the value ofone, and uses this value in place of the original:

要将一种数字类型转换成另一种,你要用当前值来初始化一个期望类型的新数字,这个数字的类型就是你的目标类型。在下面的例子中,常量twoThousand是UInt16类型,然而常量one是UInt8类型。它们不能直接相加,因为它们类型不同。所以要调用UInt16(one)来创建一个新的UInt16数字并用one的值来初始化,然后使用这个新数字来计算:

let twoThousand:UInt16=2_000

let one:UInt8=1

let twoThousandAndOne=twoThousand+UInt16(one)

Because both sides of the addition are now of typeUInt16, the addition is allowed. The output constant (twoThousandAndOne) is inferred to be of typeUInt16, because it is the sum of twoUInt16values.

现在两个数字的类型都是UInt16,可以进行相加。目标常量twoThousandAndOne的类型被推断为UInt16,因为它是两个UInt16值的和。

SomeType(ofInitialValue)is the default way to call the initializer of a Swift type and pass in an initial value. Behind the scenes,UInt16has an initializer that accepts aUInt8value, and so this initializer is used to make a newUInt16from an existingUInt8. You can’t pass inanytype here, however—it has to be a type for whichUInt16provides an initializer. Extending existing types to provide initializers that accept new types (including your own type definitions) is covered inExtensions.

SomeType(ofInitialValue)是调用 Swift 构造器并传入一个初始值的默认方法。在语言内部,UInt16有一个构造器,可以接受一个UInt8类型的值,所以这个构造器可以用现有的UInt8来创建一个新的UInt16。注意,你并不能传入任意类型的值,只能传入UInt16内部有对应构造器的值。不过你可以扩展现有的类型来让它可以接收其他类型的值(包括自定义类型),请参考扩展

Integer and Floating-Point Conversion (整数和浮点数转换)

Conversions between integer and floating-point numeric types must be made explicit:

整数和浮点数的转换必须显式指定类型:

let three=3

let pointOneFourOneFiveNine=0.14159

let pi=Double(three) +pointOneFourOneFiveNine

// pi equals 3.14159, and is inferred to be of type Double

// pi 等于 3.14159,所以被推测为 Double 类型

Here, the value of the constantthreeis used to create a new value of typeDouble, so that both sides of the addition are of the same type. Without this conversion in place, the addition would not be allowed.

这个例子中,常量three的值被用来创建一个Double类型的值,所以加号两边的数类型须相同。如果不进行转换,两者无法相加。

Floating-point to integer conversion must also be made explicit. An integer type can be initialized with a Double or Float value:

浮点数到整数的反向转换同样行,整数类型可以用Double或者Float类型来初始化:

let integerPi=Int(pi)

// integerPi equals 3, and is inferred to be of type Int

Floating-point values are always truncated when used to initialize a new integer value in this way. This means that4.75becomes4, and-3.9becomes-3.

当用这种方式来初始化一个新的整数值时,浮点值会被截断。也就是说4.75会变成4,-3.9会变成-3。

Note (注意)

The rules for combining numeric constants and variables are different from the rules for numeric literals. The literal value3can be added directly to the literal value0.14159, because number literals do not have an explicit type in and of themselves. Their type is inferred only at the point that they are evaluated by the compiler.

结合数字类常量和变量不同于结合数字类字面量。字面量3可以直接和字面量0.14159相加,因为数字字面量本身没有明确的类型。它们的类型只在编译器需要求值的时候被推测。

Type Aliases (类型别名)

Type aliasesdefine an alternative name for an existing type. You define type aliases with thetypealiaskeyword.

类型别名(type aliases)就是给现有类型定义另一个名字。你可以使用typealias关键字来定义类型别名。

Type aliases are useful when you want to refer to an existing type by a name that is contextually more appropriate, such as when working with data of a specific size from an external source:

当你想要给现有类型起一个更有意义的名字时,类型别名非常有用。假设你正在处理特定长度的外部资源的数据:

typealias AudioSample=UInt16

Once you define a type alias, you can use the alias anywhere you might use the original name:

定义了一个类型别名之后,你可以在任何使用原始名的地方使用别名:

var maxAmplitudeFound=AudioSample.min

// maxAmplitudeFound is now 0

Here,AudioSampleis defined as an alias forUInt16. Because it is an alias, the call toAudioSample.minactually callsUInt16.min, which provides an initial value of0for themaxAmplitudeFoundvariable.

本例中,AudioSample被定义为UInt16的一个别名。因为它是别名,AudioSample.min实际上是UInt16.min,所以会给maxAmplitudeFound赋一个初值0。

Booleans (布尔值)

Swift has a basicBooleantype, calledBool. Boolean values are referred to aslogical, because they can only ever be true or false. Swift provides two Boolean constant values,true and false:

Swift 有一个基本的布尔(Boolean)类型,叫做Bool。布尔值指逻辑上的值,因为它们只能是真或者假。Swift 有两个布尔常量,true和false:

let orangesAreOrange=true

let turnipsAreDelicious=false

The types oforangesAreOrangeandturnipsAreDelicioushave been inferred asBoolfrom the fact that they were initialized with Boolean literal values. As withIntandDoubleabove, you don’t need to declare constants or variables asBoolif you set them totrueorfalseas soon as you create them. Type inference helps make Swift code more concise and readable when it initializes constants or variables with other values whose type is already known.

orangesAreOrange和turnipsAreDelicious的类型会被推断为Bool,因为它们的初值是布尔字面量。就像之前提到的Int和Double一样,如果你创建变量的时候给它们赋值true或者false,那你不需要将常量或者变量声明为Bool类型。初始化常量或者变量的时候如果所赋的值类型已知,就可以触发类型推断,这让 Swift 代码更加简洁并且可读性更高。

Boolean values are particularly useful when you work with conditional statements such as theifstatement:

当你编写条件语句比如if语句的时候,布尔值非常有用:

if turnipsAreDelicious {

    print("Mmm, tasty turnips!")

} else {

    print("Eww, turnips are horrible.")

}

// 输出 "Eww, turnips are horrible."

Conditional statements such as theifstatement are covered in more detail inControl Flow.

条件语句,例如if,请参考控制流

Swift’s type safety prevents non-Boolean values from being substituted forBool. The following example reports a compile-time error:

如果你在需要使用Bool类型的地方使用了非布尔值,Swift 的类型安全机制会报错。下面的例子会报告一个编译时错误:

let i=1

if i {

// this example will not compile, and will report an error

// 这个例子不会通过编译,会报错

}

However, the alternative example below is valid:

然而,下面的例子是合法的:

let i=1

if i ==1 {

// this example will compile successfully

// 这个例子会编译成功

}

The result of thei == 1comparison is of typeBool, and so this second example passes the type-check. Comparisons likei == 1are discussed inBasic Operators.

i == 1的比较结果是Bool类型,所以第二个例子可以通过类型检查。类似i == 1这样的比较,请参考基本操作符

As with other examples of type safety in Swift, this approach avoids accidental errors and ensures that the intention of a particular section of code is always clear.

和 Swift 中的其他类型安全的例子一样,这个方法可以避免错误并保证这块代码的意图总是清晰的。

Tuples (元组)

Tuplesgroup multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.

元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。

In this example,(404, "Not Found")is a tuple that describes anHTTP status code. An HTTP status code is a special value returned by a web server whenever you request a web page. A status code of404 Not Foundis returned if you request a webpage that doesn’t exist.

下面这个例子中,(404, "Not Found")是一个描述HTTP 状态码(HTTP status code)的元组。HTTP 状态码是当你请求网页的时候 web 服务器返回的一个特殊值。如果你请求的网页不存在就会返回一个404 Not Found状态码。

let http404Error= (404,"Not Found")

// http404Error is of type (Int, String), and equals (404, "Not Found")

The(404, "Not Found")tuple groups together anIntand aStringto give the HTTP status code two separate values: a number and a human-readable description. It can be described as “a tuple of type(Int, String)”.

(404, "Not Found")元组把一个Int值和一个String值组合起来表示 HTTP 状态码的两个部分:一个数字和一个人类可读的描述。这个元组可以被描述为“一个类型为(Int, String)的元组”。

You can create tuples from any permutation of types, and they can contain as many different types as you like. There’s nothing stopping you from having a tuple of type(Int, Int, Int), or(String, Bool), or indeed any other permutation you require.

你可以把任意顺序的类型组合成一个元组,这个元组可以包含所有类型。只要你想,你可以创建一个类型为(Int, Int, Int)或者(String, Bool)或者其他任何你想要的组合的元组。

You candecomposea tuple’s contents into separate constants or variables, which you then access as usual:

你可以将一个元组的内容分解(decompose)成单独的常量和变量,然后你就可以正常使用它们了:

let (statusCode,statusMessage) =http404Error

print("The status code is\(statusCode)")

// Prints "The status code is 404"

print("The status message is\(statusMessage)")

// Prints "The status message is Not Found"

If you only need some of the tuple’s values, ignore parts of the tuple with an underscore (_) when you decompose the tuple:

如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记:

let (justTheStatusCode,_) =http404Error

print("The status code is\(justTheStatusCode)")

// Prints "The status code is 404"

Alternatively, access the individual element values in a tuple using index numbers starting at zero:

此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:

print("The status code is\(http404Error.0)")

// Prints "The status code is 404"

print("The status message is\(http404Error.1)")

// Prints "The status message is Not Found"

You can name the individual elements in a tuple when the tuple is defined:

你可以在定义元组的时候给单个元素命名:

let http200Status= (statusCode:200,description:"OK")

If you name the elements in a tuple, you can use the element names to access the values of those elements:

给元组中的元素命名后,你可以通过名字来获取这些元素的值:

print("The status code is\(http200Status.statusCode)")

// Prints "The status code is 200"

print("The status message is\(http200Status.description)")

// Prints "The status message is OK"

Tuples are particularly useful as the return values of functions. A function that tries to retrieve a web page might return the(Int, String)tuple type to describe the success or failure of the page retrieval. By returning a tuple with two distinct values, each of a different type, the function provides more useful information about its outcome than if it could only return a single value of a single type. For more information, seeFunctions with Multiple Return Values.

作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个(Int, String)元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。请参考函数参数与返回值

Note (注意)

Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple. For more information, seeClasses and Structures.

元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构。如果你的数据结构并不是临时使用,请使用类或者结构体而不是元组。请参考类和结构体

Optionals

You use optionalsin situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.

使用可选类型(optionals)来处理值可能缺失的情况。可选项表示两种可能性:有一个值,您可以解开可选值以访问该值,或者根本没有值。

Note (注意)

The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to returnnilfrom a method that would otherwise return an object, withnilmeaning “the absence of a valid object.” However, this only works for objects—it doesn’t work for structures, basic C types, or enumeration values. For these types, Objective-C methods typically return a special value (such asNSNotFound) to indicate the absence of a value. This approach assumes that the method’s caller knows there is a special value to test against and remembers to check for it. Swift’s optionals let you indicate the absence of a value forany type at all, without the need for special constants.

C 和 Objective-C 中并没有可选类型这个概念。最接近的是 Objective-C 中的一个特性,一个方法要不返回一个对象要不返回nil,nil表示“缺少一个合法的对象”。然而,这只对对象起作用——对于结构体,基本的 C 类型或者枚举类型不起作用。对于这些类型,Objective-C 方法一般会返回一个特殊值(比如NSNotFound)来暗示值缺失。这种方法假设方法的调用者知道并记得对特殊值进行判断。然而,Swift 的可选类型可以让你暗示任意类型的值缺失,并不需要一个特殊值。

Here’s an example of how optionals can be used to cope with the absence of a value. Swift’s Int type has an initializer which tries to convert a Stringvalue into an Intvalue. However, not every string can be converted into an integer. The string"123"can be converted into the numeric value123, but the string"hello, world"does not have an obvious numeric value to convert to.

来看一个例子。Swift 的Int类型有一种构造器,作用是将一个String值转换成一个Int值。然而,并不是所有的字符串都可以转换成一个整数。字符串"123"可以被转换成数字123,但是字符串"hello, world"不行。

The example below uses the initializer to try to convert aStringinto anInt:

下面的例子使用这种构造器来尝试将一个String转换成Int:

let possibleNumber="123"

let convertedNumber=Int(possibleNumber)

// convertedNumber is inferred to be of type "Int?", or "optional Int"

Because the initializer might fail, it returns anoptionalInt, rather than anInt. An optionalIntis written asInt?, notInt. The question mark indicates that the value it contains is optional, meaning that it might containsomeIntvalue, or it might containno value at all. (It can’t contain anything else, such as aBoolvalue or aStringvalue. It’s either anInt, or it’s nothing at all.)

因为该构造器可能会失败,所以它返回一个可选类型(optional)Int,而不是一个Int。一个可选的Int被写作Int?而不是Int。问号暗示包含的值是可选类型,也就是说可能包含Int值也可能不包含值。(不能包含其他任何值比如Bool值或者String值。只能是Int或者什么都没有。)

nil

You set an optional variable to a valueless state by assigning it the special valuenil:

你可以给可选变量赋值为nil来表示它没有值:

var serverResponseCode:Int? =404

// serverResponseCode contains an actual Int value of 404

serverResponseCode=nil

// serverResponseCode now contains no value

Note (注意)

nilcannot be used with nonoptional constants and variables. If a constant or variable in your code needs to work with the absence of a value under certain conditions, always declare it as an optional value of the appropriate type.

nil不能用于非可选的常量和变量。如果你的代码中有常量或者变量需要处理值缺失的情况,请把它们声明成对应的可选类型。

If you define an optional variable without providing a default value, the variable is automatically set tonilfor you:

如果你声明一个可选常量或者变量但是没有赋值,它们会自动被设置为nil:

var surveyAnswer:String?

// surveyAnswer is automatically set to nil

Note

Swift’snilis not the same asnilin Objective-C. In Objective-C,nilis a pointer to a nonexistent object. In Swift,nilis not a pointer—it is the absence of a value of a certain type. Optionals ofanytype can be set tonil, not just object types.

Swift 的nil和 Objective-C 中的nil并不一样。在 Objective-C 中,nil是一个指向不存在对象的指针。在 Swift 中,nil不是指针——它是一个确定的值,用来表示值缺失。任何类型的可选状态都可以被设置为nil,不只是对象类型。

If Statements and Forced Unwrapping (if 语句以及强制解析)

You can use anifstatement to find out whether an optional contains a value by comparing the optional againstnil. You perform this comparison with the “equal to” operator (==) or the “not equal to” operator (!=).

你可以使用if语句和nil比较来判断一个可选值是否包含值。你可以使用“相等”(==)或“不等”(!=)来执行比较。

If an optional has a value, it is considered to be “not equal to”nil:

如果可选类型有值,它将不等于nil:

if convertedNumber!=nil{

print("convertedNumber contains some integer value.")

}

// Prints "convertedNumber contains some integer value."

Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.” This is known asforced unwrappingof the optional’s value:

当你确定可选类型确实包含值之后,你可以在可选的名字后面加一个感叹号(!)来获取值。这个惊叹号表示“我知道这个可选有值,请使用它。”这被称为可选值的强制解析(forced unwrapping)

if convertedNumber!=nil{

print("convertedNumber has an integer value of\(convertedNumber!).")

}

// Prints "convertedNumber has an integer value of 123."

For more on theifstatement, seeControl Flow.

更多关于if语句的内容,请参考控制流

Note (注意)

Trying to use!to access a nonexistent optional value triggers a runtime error. Always make sure that an optional contains a non-nilvalue before using!to force-unwrap its value.

使用!来获取一个不存在的可选值会导致运行时错误。使用!来强制解析值之前,一定要确定可选包含一个非nil的值。


上一篇下一篇

猜你喜欢

热点阅读