new version
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
> 翻译:[numbbbbb](https://github.com/numbbbbb)
|
||||
> 翻译:[numbbbbb](https://github.com/numbbbbb)
|
||||
> 校对:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai)
|
||||
|
||||
# Swift 初见
|
||||
@ -25,8 +25,8 @@ println("Hello, world")
|
||||
|
||||
这个教程会通过一系列编程例子来让你对 Swift 有初步了解,如果你有什么不理解的地方也不用担心——任何本章介绍的内容都会在后面的章节中详细讲解。
|
||||
|
||||
> 注意:
|
||||
> 为了获得最好的体验,在 Xcode 当中使用代码预览功能。代码预览功能可以让你编辑代码并实时看到运行结果。
|
||||
> 注意:
|
||||
> 为了获得最好的体验,在 Xcode 当中使用代码预览功能。代码预览功能可以让你编辑代码并实时看到运行结果。
|
||||
> <a href="https://github.com/numbbbbb/the-swift-programming-language-in-chinese/raw/gh-pages/source/chapter1/GuidedTour.playground.zip">打开Playground</a>
|
||||
|
||||
<a name="simple_values"></a>
|
||||
@ -50,7 +50,7 @@ let implicitDouble = 70.0
|
||||
let explicitDouble: Double = 70
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 创建一个常量,显式指定类型为`Float`并指定初始值为4。
|
||||
|
||||
值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。
|
||||
@ -60,7 +60,7 @@ let label = "The width is"
|
||||
let width = 94
|
||||
let widthLabel = label + String(width)
|
||||
```
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 删除最后一行中的`String`,错误提示是什么?
|
||||
|
||||
有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠。例如:
|
||||
@ -72,7 +72,7 @@ let appleSummary = "I have \(apples) apples."
|
||||
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 使用`\()`来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。
|
||||
|
||||
使用方括号`[]`来创建数组和字典,并使用下标或者键(key)来访问元素。
|
||||
@ -137,7 +137,7 @@ if let name = optionalName {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 把`optionalName`改成`nil`,greeting会是什么?添加一个`else`语句,当`optionalName`是`nil`时给greeting赋一个不同的值。
|
||||
|
||||
如果变量的可选值是`nil`,条件会判断为`false`,大括号中的代码会被跳过。如果不是`nil`,会将值赋给`let`后面的常量,这样代码块中就可以使用这个值了。
|
||||
@ -158,7 +158,7 @@ default:
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 删除`default`语句,看看会有什么错误?
|
||||
|
||||
运行`switch`中匹配到的子句之后,程序会退出`switch`语句,并不会继续向下运行,所以不需要在每个子句结尾写`break`。
|
||||
@ -182,7 +182,7 @@ for (kind, numbers) in interestingNumbers {
|
||||
largest
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 添加另一个变量来记录哪种类型的数字是最大的。
|
||||
|
||||
使用`while`来重复运行一段代码直到不满足条件。循环条件可以在开头也可以在结尾。
|
||||
@ -231,7 +231,7 @@ func greet(name: String, day: String) -> String {
|
||||
greet("Bob", "Tuesday")
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 删除`day`参数,添加一个参数来表示今天吃了什么午饭。
|
||||
|
||||
使用元组来让一个函数返回多个值。该元组的元素可以用名称或数字来表示。
|
||||
@ -241,7 +241,7 @@ func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
|
||||
var min = scores[0]
|
||||
var max = scores[0]
|
||||
var sum = 0
|
||||
|
||||
|
||||
for score in scores {
|
||||
if score > max {
|
||||
max = score
|
||||
@ -250,7 +250,7 @@ func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
|
||||
}
|
||||
sum += score
|
||||
}
|
||||
|
||||
|
||||
return (min, max, sum)
|
||||
}
|
||||
let statistics = calculateStatistics([5, 3, 100, 3, 9])
|
||||
@ -272,7 +272,7 @@ sumOf()
|
||||
sumOf(42, 597, 12)
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 写一个计算参数平均值的函数。
|
||||
|
||||
函数可以嵌套。被嵌套的函数可以访问外侧函数的变量,你可以使用嵌套函数来重构一个太长或者太复杂的函数。
|
||||
@ -330,7 +330,7 @@ numbers.map({
|
||||
})
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 重写闭包,对所有奇数返回0。
|
||||
|
||||
有很多种创建闭包的方法。如果一个闭包的类型已知,比如作为一个回调函数,你可以忽略参数的类型和返回值。单个语句闭包会把它语句的值当做结果返回。
|
||||
@ -361,7 +361,7 @@ class Shape {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 使用`let`添加一个常量属性,再添加一个接收一个参数的方法。
|
||||
|
||||
要创建一个类的实例,在类名后面加上括号。使用点语法来访问实例的属性和方法。
|
||||
@ -420,7 +420,7 @@ test.area()
|
||||
test.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 创建`NamedShape`的另一个子类`Circle`,构造器接收两个参数,一个是半径一个是名称,实现`area`和`describe`方法。
|
||||
|
||||
属性可以有 getter 和 setter 。
|
||||
@ -539,7 +539,7 @@ let ace = Rank.Ace
|
||||
let aceRawValue = ace.rawValue
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 写一个函数,通过比较它们的原始值来比较两个`Rank`值。
|
||||
|
||||
在上面的例子中,枚举原始值的类型是`Int`,所以你只需要设置第一个原始值。剩下的原始值会按照顺序赋值。你也可以使用字符串或者浮点数作为枚举的原始值。
|
||||
@ -575,7 +575,7 @@ let hearts = Suit.Hearts
|
||||
let heartsDescription = hearts.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 给`Suit`添加一个`color`方法,对`spades`和`clubs`返回“black”,对`hearts`和`diamonds`返回“red”。
|
||||
|
||||
注意,有两种方式可以引用`Hearts`成员:给`hearts`常量赋值时,枚举成员`Suit.Hearts`需要用全名来引用,因为常量没有显式指定类型。在`switch`里,枚举成员使用缩写`.Hearts`来引用,因为`self`的值已经知道是一个`suit`。已知变量类型的情况下你可以使用缩写。
|
||||
@ -595,7 +595,7 @@ let threeOfSpades = Card(rank: .Three, suit: .Spades)
|
||||
let threeOfSpadesDescription = threeOfSpades.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 给`Card`添加一个方法,创建一副完整的扑克牌并把每张牌的 rank 和 suit 对应起来。
|
||||
|
||||
一个枚举成员的实例可以有实例值。相同枚举成员的实例可以有不同的值。创建实例的时候传入值即可。实例值和原始值是不同的:枚举成员的原始值对于所有实例都是相同的,而且你是在定义枚举的时候设置原始值。
|
||||
@ -619,7 +619,7 @@ case let .Error(error):
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 给`ServerResponse`和`switch`添加第三种情况。
|
||||
|
||||
注意如何从`ServerResponse`中提取日升和日落时间。
|
||||
@ -661,12 +661,12 @@ b.adjust()
|
||||
let bDescription = b.simpleDescription
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 写一个实现这个协议的枚举。
|
||||
|
||||
注意声明`SimpleStructure`时候`mutating`关键字用来标记一个会修改结构体的方法。`SimpleClass`的声明不需要标记任何方法因为类中的方法经常会修改类。
|
||||
|
||||
使用`extension`来为现有的类型添加功能,比如新的方法和参数。你可以使用扩展来改造定义在别处,甚至是从外部库或者框架引入的一个类型,使得这个类型遵循某个协议。
|
||||
使用`extension`来为现有的类型添加功能,比如新的方法和参数。你可以使用扩展在别处修改定义,甚至是从外部库或者框架引入的一个类型,使得这个类型遵循某个协议。
|
||||
|
||||
```swift
|
||||
extension Int: ExampleProtocol {
|
||||
@ -680,7 +680,7 @@ extension Int: ExampleProtocol {
|
||||
7.simpleDescription
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 给`Double`类型写一个扩展,添加`absoluteValue`功能。
|
||||
|
||||
你可以像使用其他命名类型一样使用协议名——例如,创建一个有不同类型但是都实现一个协议的对象集合。当你处理类型是协议的值时,协议外定义的方法不可用。
|
||||
@ -737,7 +737,7 @@ func anyCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator
|
||||
anyCommonElements([1, 2, 3], [3])
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习:
|
||||
> 修改`anyCommonElements`函数来创建一个函数,返回一个数组,内容是两个序列的共有元素。
|
||||
|
||||
简单起见,你可以忽略`where`,只在冒号后面写协议或者类名。` <T: Equatable>`和`<T where T: Equatable>`是等价的。
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -150,7 +150,7 @@ for item in library {
|
||||
Swift为不确定类型提供了两种特殊类型别名:
|
||||
|
||||
* `AnyObject`可以代表任何class类型的实例。
|
||||
* `Any`可以表示任何类型,除了方法类型(function types)。
|
||||
* `Any`可以表示任何类型,包括方法类型(function types)。
|
||||
|
||||
> 注意:
|
||||
只有当你明确的需要它的行为和功能时才使用`Any`和`AnyObject`。在你的代码里使用你期望的明确的类型总是更好的。
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
159
source/theme/style/css/xcode.css
Normal file
159
source/theme/style/css/xcode.css
Normal file
@ -0,0 +1,159 @@
|
||||
/*
|
||||
|
||||
XCode style (c) Angel Garcia <angelgarcia.mail@gmail.com>
|
||||
|
||||
*/
|
||||
|
||||
.hljs {
|
||||
display: block;
|
||||
overflow-x: auto;
|
||||
padding: 0.5em;
|
||||
background: #fff;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.hljs-comment,
|
||||
.hljs-template_comment,
|
||||
.hljs-javadoc {
|
||||
color: #006a00;
|
||||
}
|
||||
|
||||
.hljs-keyword,
|
||||
.hljs-literal,
|
||||
.nginx .hljs-title {
|
||||
color: #aa0d91;
|
||||
}
|
||||
.method,
|
||||
.hljs-list .hljs-title,
|
||||
.hljs-tag .hljs-title,
|
||||
.setting .hljs-value,
|
||||
.hljs-winutils,
|
||||
.tex .hljs-command,
|
||||
.http .hljs-title,
|
||||
.hljs-request,
|
||||
.hljs-status {
|
||||
color: #008;
|
||||
}
|
||||
|
||||
.hljs-envvar,
|
||||
.tex .hljs-special {
|
||||
color: #660;
|
||||
}
|
||||
|
||||
.hljs-string {
|
||||
color: #c41a16;
|
||||
}
|
||||
.hljs-tag .hljs-value,
|
||||
.hljs-cdata,
|
||||
.hljs-filter .hljs-argument,
|
||||
.hljs-attr_selector,
|
||||
.apache .hljs-cbracket,
|
||||
.hljs-date,
|
||||
.hljs-regexp {
|
||||
color: #080;
|
||||
}
|
||||
|
||||
.hljs-sub .hljs-identifier,
|
||||
.hljs-pi,
|
||||
.hljs-tag,
|
||||
.hljs-tag .hljs-keyword,
|
||||
.hljs-decorator,
|
||||
.ini .hljs-title,
|
||||
.hljs-shebang,
|
||||
.hljs-prompt,
|
||||
.hljs-hexcolor,
|
||||
.hljs-rules .hljs-value,
|
||||
.hljs-symbol,
|
||||
.hljs-symbol .hljs-string,
|
||||
.hljs-number,
|
||||
.css .hljs-function,
|
||||
.clojure .hljs-title,
|
||||
.clojure .hljs-built_in,
|
||||
.hljs-function .hljs-title,
|
||||
.coffeescript .hljs-attribute {
|
||||
color: #1c00cf;
|
||||
}
|
||||
|
||||
.hljs-class .hljs-title,
|
||||
.haskell .hljs-type,
|
||||
.smalltalk .hljs-class,
|
||||
.hljs-javadoctag,
|
||||
.hljs-yardoctag,
|
||||
.hljs-phpdoc,
|
||||
.hljs-typename,
|
||||
.hljs-tag .hljs-attribute,
|
||||
.hljs-doctype,
|
||||
.hljs-class .hljs-id,
|
||||
.hljs-built_in,
|
||||
.setting,
|
||||
.hljs-params,
|
||||
.clojure .hljs-attribute {
|
||||
color: #5c2699;
|
||||
}
|
||||
|
||||
.hljs-variable {
|
||||
color: #3f6e74;
|
||||
}
|
||||
.css .hljs-tag,
|
||||
.hljs-rules .hljs-property,
|
||||
.hljs-pseudo,
|
||||
.hljs-subst {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
.css .hljs-class,
|
||||
.css .hljs-id {
|
||||
color: #9b703f;
|
||||
}
|
||||
|
||||
.hljs-value .hljs-important {
|
||||
color: #ff7700;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.hljs-rules .hljs-keyword {
|
||||
color: #c5af75;
|
||||
}
|
||||
|
||||
.hljs-annotation,
|
||||
.apache .hljs-sqbracket,
|
||||
.nginx .hljs-built_in {
|
||||
color: #9b859d;
|
||||
}
|
||||
|
||||
.hljs-preprocessor,
|
||||
.hljs-preprocessor *,
|
||||
.hljs-pragma {
|
||||
color: #643820;
|
||||
}
|
||||
|
||||
.tex .hljs-formula {
|
||||
background-color: #eee;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.diff .hljs-header,
|
||||
.hljs-chunk {
|
||||
color: #808080;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.diff .hljs-change {
|
||||
background-color: #bccff9;
|
||||
}
|
||||
|
||||
.hljs-addition {
|
||||
background-color: #baeeba;
|
||||
}
|
||||
|
||||
.hljs-deletion {
|
||||
background-color: #ffc8bd;
|
||||
}
|
||||
|
||||
.hljs-comment .hljs-yardoctag {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.method .hljs-id {
|
||||
color: #000;
|
||||
}
|
||||
1
source/theme/style/js/highlight.pack.js
Normal file
1
source/theme/style/js/highlight.pack.js
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user