Swift also makes extensive user of variables whose values cannot be changed.
在Swift中,值不可比变的变量有着广泛的应用。
Constants are used throughout Swift to make code safer and clearer in intent when you work with valuese that do not need to change.
“意图”(in intent)
在Swift中,如果你要处理的值不需要改变,那使用常量可以让你的代码更加安全并且更好的表达你的意图。
In addition to familiar types,Swift introduces advanced types not found in Object-c.
除了我们熟悉的类型,Swift还增加了 Objective-C中没有的类型
tuple元组
Swift also introduces optional types ,which handle the absence of a value.
Swift 增加了可选类型,用于处理值缺失的情况。
OPtional are examples of fact that Swift is a type safe language.
Swift 是一个类型安全的语言,可选就是一个很好的之一。
You can declare multiple constants or multiple variabkles on a single line,separated by commas:
你可以在一行中声明多个常量或者变量,用逗号隔开。
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.
如果需要添加类型标注,需要在常量或者变量后面加一个冒号和一个空格,然后加上类型名称。
Naming Constants and Variables 常量和变量的命名。
private-use (or invalid )Unicode points.保留的(或者非法的)Unicode码位。
Printing Constants and Variables 输出常量和变量。
two forward-slashes
两个正斜杠
semicolons
n.分号( semicolon的名词复数 );
Type Safety and Type Inference
类型安全和类型推测
Numeric Literals 数值型字面量
exponent 指数
They cannot be added together directly,because they are not of the same type .
他们不能直接相加,因为它们类型不同。
You cannot pass in any type here ,however ---it has to be a type for which UInt16 provides an initializer.
你并能传入任何类型的值,只能传入Unit16内部有对应构造器的值。
Conversions between integer and floating-point numeric types must be made explicit.
整数和浮点类型的转换必须显式指定类型。
Floating-point values are always truncated when used to initialize a new integer value in this way.
当使用这种方式来初始化一个新的整数值的时候,浮点值会被截断。
Type Aliases 类型别名
Swift's type safety prevents non-Boolean values from being be substitued for Bool.
如果你在需要使用Bool类型的地方使用了非布尔类型,swift的安全机制会报错。
As with other examples of type safety in Swift,this approach avoids accidental errors
and ensures that the intention of code is always clear.
和Swift 中的其他类型例子一样,这个方法可以避免错误并保证这块代码的意图总是清晰的。
Tuples group multiple values into a single compound value.
元组將多个值组合成一个复合值。
You can create tuples from any permutation of types ,and they can contain as many different type as you like.
你可以把任意顺序的类型组合成一个元组,这个元组可以包含所有的类型。
If your data structures is likely to persist beyond a temporary scope,model it as a class or structure,rather than as a tuple.
如果你的数据结构并不是临时使用,请使用类或者结构体而不是元组。
If Statements and Forced Unwrapping
if语句和强制解析
OPtional Binding 可选绑定
If you define an optional constant or variable withoue providing a default value,the
constant or variable is automaticlly set to nil for you.
如果你声明一个可选常量或者变量但是没有赋值,它们会自动被设置成nil。
Implicitly Unwrapped Optionals
隐式解析可选
Optional can be checked with an if statement to see if a values exists,and can be
conditionally unwrapped with optional binding to access the optinal's value if it does
exist.
可以通过if语句来判断是否有值,如果有值可以通过绑定来解析值。
This kinds of optionals are defined as implicitly unwrapped optionals.
这种类型的可选定义为隐式解析可选。
Debugging with Assertions
使用断言调试
You can use an assertion to make sure that an essential condition is satisfied before
excuting any future code.
你可以使用断言来保证在运行其他代码之前,某些重要条件已经满足。
If the condition evaluates to true ,code execution continues as usual;
if the condition evaluates to false,code execution ends,and your app is terminated.
如果判断条件为真,代码將继续运行;如果判断条件为假,代码运行停止,你的应用被终止。