make the structure standard
This commit is contained in:
@ -1,8 +1,40 @@
|
||||
# Summary
|
||||
|
||||
* [欢迎使用 Swift](chapter1/README.md)
|
||||
* [关于 Swift](chapter1/swift.md)
|
||||
* [Swift 初见](chapter1/a_swift_tour.md)
|
||||
* [Swift 教程](chapter2/the_basics.md)
|
||||
* [基础部分](chapter2/article_1.md)
|
||||
* [欢迎使用 Swift](chapter1/chapter1.md)
|
||||
* [关于 Swift](chapter1/01_swift.md)
|
||||
* [Swift 初见](chapter1/02_a_swift_tour.md)
|
||||
* [Swift 教程](chapter2/chapter2.md)
|
||||
* [基础部分](chapter2/01_The_Basics.md)
|
||||
* [基本操作符](chapter2/02_Basic_Operators.md)
|
||||
* [字符串和字符](chapter2/03_Strings_and_Characters.md)
|
||||
* [集合类型](chapter2/04_Collection_Types.md)
|
||||
* [控制流](chapter2/05_Control_Flow.md)
|
||||
* [函数](chapter2/06_Functions.md)
|
||||
* [闭包](chapter2/07_Closures.md)
|
||||
* [枚举](chapter2/08_Enumerations.md)
|
||||
* [类和结构体](chapter2/09_Classes_and_Structures.md)
|
||||
* [属性](chapter2/10_Properties.md)
|
||||
* [方法](chapter2/11_Methods.md)
|
||||
* [下标](chapter2/12_Subscripts.md)
|
||||
* [继承](chapter2/13_Inheritance.md)
|
||||
* [构造函数](chapter2/14_Initialization.md)
|
||||
* [析构函数](chapter2/15_Deinitialization.md)
|
||||
* [自动引用计数](chapter2/16_Automatic_Reference_Counting.md)
|
||||
* [可选链](chapter2/17_Optional_Chaining.md)
|
||||
* [类型检查](chapter2/18_Type_Casting.md)
|
||||
* [嵌套类型](chapter2/19_Nested_Types.md)
|
||||
* [扩展](chapter2/20_Extensions.md)
|
||||
* [接口](chapter2/21_Protocols.md)
|
||||
* [泛型](chapter2/22_Generics.md)
|
||||
* [高级操作符](chapter2/23_Advanced_Operators.md)
|
||||
* [语言参考](chapter3/chapter3.md)
|
||||
* [关于语言参考](chapter3/01_About_the_Language_Reference.md)
|
||||
* [词法结构](chapter3/02_Lexical_Structure.md)
|
||||
* [类型](chapter3/03_Types.md)
|
||||
* [表达式](chapter3/04_Expressions.md)
|
||||
* [声明](chapter3/05_Declarations.md)
|
||||
* [属性](chapter3/06_Attributes.md)
|
||||
* [模式](chapter3/07_Patterns.md)
|
||||
* [泛型参数](chapter3/08_Generic_Parameters_and_Arguments.md)
|
||||
* [语法总结](chapter3/09_Summary_of_the_Grammar.md)
|
||||
|
||||
|
||||
@ -59,9 +59,9 @@ Swift 是一个类型安全的语言,可选就是一个很好的例子。Swift
|
||||
|
||||
你可以用任何你喜欢的字符作为常量和变量名,包括Unicode字符:
|
||||
|
||||
let π = 3.14159
|
||||
let 你好 = "你好世界"
|
||||
let 🐶🐮 = "dogcow"
|
||||
let π = 3.14159
|
||||
let 你好 = "你好世界"
|
||||
let 🐶🐮 = "dogcow"
|
||||
|
||||
常量与变量名不能包含数学符号,箭头,保留的(或者非法的)Unicode码位,连线与制表符。尽管常量与变量名中可以包含数字,但是它们不能以数字打头。
|
||||
|
||||
@ -71,35 +71,35 @@ Swift 是一个类型安全的语言,可选就是一个很好的例子。Swift
|
||||
|
||||
你可以更改现有的变量值为其他同类型的值,在下面的例子中,`friendlyWelcome`的值从`"Hello!"`改为了`"Bonjour!"`:
|
||||
|
||||
var friendlyWelcome = "Hello!"
|
||||
friendlyWelcome = "Bonjour!"
|
||||
// friendlyWelcome is now "Bonjour!"
|
||||
var friendlyWelcome = "Hello!"
|
||||
friendlyWelcome = "Bonjour!"
|
||||
// friendlyWelcome is now "Bonjour!"
|
||||
|
||||
和变量不一样,常量的值一旦被确定以后就不能更改了。尝试这样做会在编译时报错:
|
||||
|
||||
let languageName = "Swift"
|
||||
languageName = "Swift++"
|
||||
// this is a compile-time error - languageName cannot be changed
|
||||
|
||||
let languageName = "Swift"
|
||||
languageName = "Swift++"
|
||||
// this is a compile-time error - languageName cannot be changed
|
||||
|
||||
### 输出常量和变量
|
||||
|
||||
你可以用`println`函数来输出当前常量或变量的值:
|
||||
|
||||
println(friendlyWelcome)
|
||||
// prints "Bonjour!"
|
||||
|
||||
println(friendlyWelcome)
|
||||
// prints "Bonjour!"
|
||||
|
||||
`println`是一个用来输出的全局函数,输出的内容会在最后带换行。如果你用Xcode,`println`将会输出内容到“console”面板上。(另一种函数叫`print`,唯一区别是在输出内容最后不会加入换行。)
|
||||
|
||||
`println`函数输出传入的`String`值:
|
||||
|
||||
println("This is a string")
|
||||
// prints "This is a string"
|
||||
println("This is a string")
|
||||
// prints "This is a string"
|
||||
|
||||
像Cocoa里的`NSLog`函数一样,`println`函数可以输出更复杂的信息。这些信息可以包含当前常量和变量的值。
|
||||
|
||||
Swift用字符串插值(string interpolation)的方式把常量名或者变量名当做占位符加入到长字符串中,Swift会用当前常量或变量的值替换这些占位符。将常量或变量名放入反斜杠符加一对圆括号中`"\()"`:
|
||||
|
||||
println("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
// prints "The current value of friendlyWelcome is Bonjour!
|
||||
|
||||
println("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
// prints "The current value of friendlyWelcome is Bonjour!
|
||||
|
||||
> 注意:字符串插值所有可用的选项在 字符串插值 这章中讲述。
|
||||
0
source/chapter2/02_Basic_Operators.md
Normal file
0
source/chapter2/02_Basic_Operators.md
Normal file
@ -30,10 +30,10 @@ let someString = "Some string literal value"
|
||||
|
||||
字符串字面量可以包含以下特殊字符:
|
||||
|
||||
* 转移特殊字符 `\0` (空字符)、`\\`(反斜线)、`\t` (水平制表符)、`\n` (换行符)、`\r` (回车符)、`\"` (双引号)、`\'` (单引号)。
|
||||
* 单字节 Unicode 标量,写成 `\xnn`,其中 nn 为两位十六进制数。
|
||||
* 双字节 Unicode 标量,写成 `\unnnn`,其中 nnnn 为四位十六进制数。
|
||||
* 四字节 Unicode 标量,写成 `\Unnnnnnnn`,其中 nnnnnnnn 为八位十六进制数。
|
||||
* 转移特殊字符 `\0` (空字符)、`\\`(反斜线)、`\t` (水平制表符)、`\n` (换行符)、`\r` (回车符)、`\"` (双引号)、`\'` (单引号)。
|
||||
* 单字节 Unicode 标量,写成 `\xnn`,其中 nn 为两位十六进制数。
|
||||
* 双字节 Unicode 标量,写成 `\unnnn`,其中 nnnn 为四位十六进制数。
|
||||
* 四字节 Unicode 标量,写成 `\Unnnnnnnn`,其中 nnnnnnnn 为八位十六进制数。
|
||||
|
||||
下面的代码为各种特殊字符的使用示例。
|
||||
|
||||
@ -268,9 +268,9 @@ Swift 提供了几种不同的方式来访问字符串的 Unicode 表示。
|
||||
|
||||
另外,能够以其他三种 Unicode 兼容的方式访问字符串的值:
|
||||
|
||||
* UTF-8 代码单元集合 (利用字符串的 `utf8` 属性进行访问)
|
||||
* UTF-16 代码单元集合 (利用字符串的 `utf16` 属性进行访问)
|
||||
* 21位的 Unicode 标量值集合 (利用字符串的 `unicodeScalars` 属性进行访问)
|
||||
* UTF-8 代码单元集合 (利用字符串的 `utf8` 属性进行访问)
|
||||
* UTF-16 代码单元集合 (利用字符串的 `utf16` 属性进行访问)
|
||||
* 21位的 Unicode 标量值集合 (利用字符串的 `unicodeScalars` 属性进行访问)
|
||||
|
||||
下面由 `D` `o` `g` `!` 和 `🐶` (狗脸表情,Unicode 标量为 `U+1F436`)组成的字符串中的每一个字符代表着一种不同的表示:
|
||||
|
||||
0
source/chapter2/04_Collection_Types.md
Normal file
0
source/chapter2/04_Collection_Types.md
Normal file
0
source/chapter2/05_Control_Flow.md
Normal file
0
source/chapter2/05_Control_Flow.md
Normal file
@ -14,20 +14,20 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
|
||||
在下面例子中的函数叫做`"greetingForPerson"`,之所以叫这个名字是因为这个函数用一个人的名字当做输入,并返回给这个人的问候语。为了完成这个任务,你定义一个输入参数-一个叫做`personName`的`String`值,和一个包含给这个人问候语的`String`类型的返回值:
|
||||
|
||||
func sayHello(personName: String) -> String {
|
||||
let greeting = "Hello, " + personName + "!"
|
||||
return greeting
|
||||
}
|
||||
func sayHello(personName: String) -> String {
|
||||
let greeting = "Hello, " + personName + "!"
|
||||
return greeting
|
||||
}
|
||||
|
||||
所有的这些信息汇总起来成为函数的定义,并以`func`作为前缀。指定函数返回类型时,用返回箭头`->`(一个连字符后跟一个右尖括号)后跟返回类型的名称的方式来表示。
|
||||
|
||||
该定义描述了函数做什么,它期望接收什么和执行结束时它返回的结果是什么。这样的定义使的函数可以在别的地方以一种清晰的方式被调用:
|
||||
|
||||
println(sayHello("Anna"))
|
||||
// prints "Hello, Anna!"
|
||||
println(sayHello("Brian"))
|
||||
// prints "Hello, Brian!
|
||||
|
||||
println(sayHello("Anna"))
|
||||
// prints "Hello, Anna!"
|
||||
println(sayHello("Brian"))
|
||||
// prints "Hello, Brian!
|
||||
|
||||
调用`sayHello`函数时,在圆括号中传给它一个`String`类型的实参。因为这个函数返回一个`String`类型的值,`sayHello`可以被包含在`println`的调用中,用来输出这个函数的返回值,正如上面所示。
|
||||
|
||||
在`sayHello`的函数体中,先定义了一个新的名为`greeting`的`String`常量,同时赋值了给`personName`的一个简单问候消息。然后用`return`关键字把这个问候返回出去。一旦`return greeting`被调用,该函数结束它的执行并返回`greeting`的当前值。
|
||||
@ -36,10 +36,10 @@ Swift统一的函数语法足够灵活,可以用来表示任何函数,包括
|
||||
|
||||
为了简化这个函数的定义,可以将问候消息的创建和返回写成一句:
|
||||
|
||||
func sayHelloAgain(personName: String) -> String {
|
||||
return "Hello again, " + personName + "!"
|
||||
}
|
||||
println(sayHelloAgain("Anna"))
|
||||
// prints "Hello again, Anna!
|
||||
func sayHelloAgain(personName: String) -> String {
|
||||
return "Hello again, " + personName + "!"
|
||||
}
|
||||
println(sayHelloAgain("Anna"))
|
||||
// prints "Hello again, Anna!
|
||||
|
||||
## 函数参数与返回值
|
||||
0
source/chapter2/07_Closures.md
Normal file
0
source/chapter2/07_Closures.md
Normal file
0
source/chapter2/08_Enumerations.md
Normal file
0
source/chapter2/08_Enumerations.md
Normal file
0
source/chapter2/09_Classes_and_Structures.md
Normal file
0
source/chapter2/09_Classes_and_Structures.md
Normal file
0
source/chapter2/10_Properties.md
Normal file
0
source/chapter2/10_Properties.md
Normal file
0
source/chapter2/11_Methods.md
Normal file
0
source/chapter2/11_Methods.md
Normal file
0
source/chapter2/12_Subscripts.md
Normal file
0
source/chapter2/12_Subscripts.md
Normal file
0
source/chapter2/13_Inheritance.md
Normal file
0
source/chapter2/13_Inheritance.md
Normal file
0
source/chapter2/14_Initialization.md
Normal file
0
source/chapter2/14_Initialization.md
Normal file
0
source/chapter2/15_Deinitialization.md
Normal file
0
source/chapter2/15_Deinitialization.md
Normal file
0
source/chapter2/16_Automatic_Reference_Counting.md
Normal file
0
source/chapter2/16_Automatic_Reference_Counting.md
Normal file
0
source/chapter2/17_Optional_Chaining.md
Normal file
0
source/chapter2/17_Optional_Chaining.md
Normal file
0
source/chapter2/18_Type_Casting.md
Normal file
0
source/chapter2/18_Type_Casting.md
Normal file
0
source/chapter2/19_Nested_Types.md
Normal file
0
source/chapter2/19_Nested_Types.md
Normal file
0
source/chapter2/20_Extensions.md
Normal file
0
source/chapter2/20_Extensions.md
Normal file
0
source/chapter2/21_Protocols.md
Normal file
0
source/chapter2/21_Protocols.md
Normal file
0
source/chapter2/22_Generics.md
Normal file
0
source/chapter2/22_Generics.md
Normal file
0
source/chapter2/23_Advanced_Operators.md
Normal file
0
source/chapter2/23_Advanced_Operators.md
Normal file
0
source/chapter3/01_About_the_Language_Reference.md
Normal file
0
source/chapter3/01_About_the_Language_Reference.md
Normal file
0
source/chapter3/02_Lexical_Structure.md
Normal file
0
source/chapter3/02_Lexical_Structure.md
Normal file
0
source/chapter3/03_Types.md
Normal file
0
source/chapter3/03_Types.md
Normal file
0
source/chapter3/04_Expressions.md
Normal file
0
source/chapter3/04_Expressions.md
Normal file
0
source/chapter3/05_Declarations.md
Normal file
0
source/chapter3/05_Declarations.md
Normal file
0
source/chapter3/06_Attributes.md
Normal file
0
source/chapter3/06_Attributes.md
Normal file
0
source/chapter3/07_Patterns.md
Normal file
0
source/chapter3/07_Patterns.md
Normal file
0
source/chapter3/09_Summary_of_the_Grammar.md
Normal file
0
source/chapter3/09_Summary_of_the_Grammar.md
Normal file
0
source/chapter3/chapter3.md
Normal file
0
source/chapter3/chapter3.md
Normal file
Reference in New Issue
Block a user