22
22
本章讨论了 Swift 语言本身定义的类型,并描述了 Swift 的类型推断行为。
23
23
24
24
> 类型的语法:
25
- > > * type* → * function-type* \ > * type* → * array-type* \ > * type* → * dictionary-type* \ > * type* → * type-identifier* \ > * type* → * tuple-type* \ > * type* → * optional-type* \ > * type* → * implicitly-unwrapped-optional-type* \ > * type* → * protocol-composition-type* \ > * type* → * opaque-type* \ > * type* → * boxed-protocol-type* \ > * type* → * metatype-type* \ > * type* → * any-type* \ > * type* → * self-type* \ > * type* → ** ` ( ` ** * type* ** ` ) ` **
25
+ >
26
+ > * type* → * function-type* \
27
+ > * type* → * array-type* \
28
+ > * type* → * dictionary-type* \
29
+ > * type* → * type-identifier* \
30
+ > * type* → * tuple-type* \
31
+ > * type* → * optional-type* \
32
+ > * type* → * implicitly-unwrapped-optional-type* \
33
+ > * type* → * protocol-composition-type* \
34
+ > * type* → * opaque-type* \
35
+ > * type* → * boxed-protocol-type* \
36
+ > * type* → * metatype-type* \
37
+ > * type* → * any-type* \
38
+ > * type* → * self-type* \
39
+ > * type* → ** ` ( ` ** * type* ** ` ) ` **
26
40
27
41
## 类型注解
28
42
@@ -40,7 +54,8 @@ func someFunction(a: Int) { /* ... */ }
40
54
类型注解可以在类型之前包含一个可选的类型属性列表。
41
55
42
56
> 类型注解的语法:
43
- > > * type-annotation* → ** ` : ` ** * attributes* _ ?_ * type*
57
+ >
58
+ > * type-annotation* → ** ` : ` ** * attributes* _ ?_ * type*
44
59
45
60
## 类型标识符
46
61
@@ -75,6 +90,8 @@ someTuple = (left: 5, right: 5) // 错误: 名称不匹配
75
90
76
91
除了 Void 是空元组类型 () 的类型别名外,所有元组类型都包含两个或更多类型。
77
92
93
+ ## 函数类型
94
+
78
95
函数类型表示函数、方法或闭包的类型,由一个参数类型和返回类型组成,用箭头(->)分隔:
79
96
80
97
``` swift
@@ -162,13 +179,17 @@ func takesTwoFunctions(first: (() -> Void) -> Void, second: (() -> Void) -> Void
162
179
如果你需要避免这个限制,可以将其中一个参数标记为逃逸的,或者使用 ` withoutActuallyEscaping(_:do:) ` 函数临时将其中一个非逃逸函数参数转换为逃逸函数。有关避免内存访问冲突的信息,请参阅 < doc:MemorySafety > 。
163
180
164
181
> 函数类型的语法:
165
- > > * function-type* → * attributes* _ ?_ * function-type-argument-clause* ** ` async ` ** _ ?_ * throws-clause* _ ?_ ** ` -> ` ** * type*
166
- > > * function-type-argument-clause* → ** ` ( ` ** ** ` ) ` ** \
182
+ >
183
+ > * function-type* → * attributes* _ ?_ * function-type-argument-clause* ** ` async ` ** _ ?_ * throws-clause* _ ?_ ** ` -> ` ** * type*
184
+ >
185
+ > * function-type-argument-clause* → ** ` ( ` ** ** ` ) ` ** \
167
186
> * function-type-argument-clause* → ** ` ( ` ** * function-type-argument-list* ** ` ... ` ** _ ?_ ** ` ) ` **
168
- > > * function-type-argument-list* → * function-type-argument* | * function-type-argument* ** ` , ` ** * function-type-argument-list* \
187
+ >
188
+ > * function-type-argument-list* → * function-type-argument* | * function-type-argument* ** ` , ` ** * function-type-argument-list* \
169
189
> * function-type-argument* → * attributes* _ ?_ * parameter-modifier* _ ?_ * type* | * argument-label* * type-annotation* \
170
190
> * argument-label* → * identifier*
171
- > > * throws-clause* → ** ` throws ` ** | ** ` throws ` ** ** ` ( ` ** * type* ** ` ) ` **
191
+ >
192
+ > * throws-clause* → ** ` throws ` ** | ** ` throws ` ** ** ` ( ` ** * type* ** ` ) ` **
172
193
173
194
func polymorphicF<T >(a: Int) -> T { return a } 是一个泛型函数,可以返回任何类型的值。而 monomorphicF(a: Int) -> Int { return a } 是一个单态函数,只能返回 Int 类型的值。
174
195
@@ -208,7 +229,8 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
208
229
有关 Swift 标准库 ` Array ` 类型的详细讨论,请参阅 < doc:CollectionTypes#数组 > 。
209
230
210
231
> 数组类型的语法:
211
- > > * array-type* → ** ` [ ` ** * type* ** ` ] ` **
232
+ >
233
+ > * array-type* → ** ` [ ` ** * type* ** ` ] ` **
212
234
213
235
## 字典类型
214
236
@@ -236,9 +258,8 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
236
258
有关 Swift 标准库 ` Dictionary ` 类型的详细讨论,请参阅 < doc:CollectionTypes#字典 > 。
237
259
238
260
> 字典类型的语法:
239
- > > * dictionary-type* → ** ` [ ` ** * type* ** ` : ` ** * type* ** ` ] ` **
240
-
241
- ## 可选类型
261
+ >
262
+ > * dictionary-type* → ** ` [ ` ** * type* ** ` : ` ** * type* ** ` ] ` **
242
263
243
264
## 可选类型
244
265
@@ -267,7 +288,8 @@ optionalInteger! // 42
267
288
有关更多信息和示例说明如何使用可选类型,请参阅 < doc:TheBasics#可选项 > 。
268
289
269
290
> 可选类型的语法:
270
- > > * optional-type* → * type* ** ` ? ` **
291
+ >
292
+ > * optional-type* → * type* ** ` ? ` **
271
293
272
294
## 隐式解包可选类型
273
295
@@ -296,7 +318,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
296
318
297
319
使用可选链来有条件地对隐式解包可选表达式执行操作。如果值为 ` nil ` ,则不执行任何操作,因此也不会产生运行时错误。
298
320
299
- 有关隐式解包可选类型的更多信息,请参阅 < doc:TheBasics#隐式解析可选类型 > 。
321
+ 有关隐式解包可选类型的更多信息,请参阅 < doc:TheBasics#隐式解包可选 > 。
300
322
301
323
为参数编写不透明类型是使用泛型类型的一种语法糖,并且没有为泛型类型参数指定名称。这个隐式的泛型类型参数有一个约束,要求它遵循不透明类型中指定的协议。如果你编写了多个不透明类型,每一个都会创建自己的泛型类型参数。例如,下面的声明是等价的:
302
324
@@ -417,7 +439,8 @@ let anotherInstance = metatype.init(string: "some string")
417
439
```swifttest -> class AnotherSubClass: SomeBaseClass { let string: String required init(string: String) { self.string = string } override class func printClassName() { print("AnotherSubClass") } } -> let metatype: AnotherSubClass.Type = AnotherSubClass.self -> let anotherInstance = metatype.init(string: "some string") ``` -->
418
440
419
441
> 元类型语法:
420
- > > * metatype-type* → * type* ** ` . ` ** ** ` Type ` ** | * type* ** ` . ` ** ** ` Protocol ` **
442
+ >
443
+ > * metatype-type* → * type* ** ` . ` ** ** ` Type ` ** | * type* ** ` . ` ** ** ` Protocol ` **
421
444
422
445
## Any 类型
423
446
@@ -461,7 +484,8 @@ if let first = mixed.first as? String {
461
484
有关更多信息,请参阅 < doc:Protocols#类专属协议 > 和 [ ` AnyObject ` ] ( https://developer.apple.com/documentation/swift/anyobject ) 。
462
485
463
486
> Any 类型语法:
464
- > > * any-type* → ** ` Any ` **
487
+ >
488
+ > * any-type* → ** ` Any ` **
465
489
466
490
## Self 类型
467
491
@@ -500,7 +524,10 @@ if let first = mixed.first as? String {
500
524
` Self ` 类型指的是与 Swift 标准库中的 ` type(of:) ` 函数相同的类型。写 ` Self.someStaticMember ` 来访问当前类型的成员与写 ` type(of: self).someStaticMember ` 是一样的。
501
525
502
526
> Self 类型的语法:
503
- > > * self-type* → ** ` Self ` **
527
+ >
528
+ > * self-type* → ** ` Self ` **
529
+
530
+ ## 类型继承子句
504
531
505
532
* 类型继承子句* 用于指定命名类型继承自哪个类以及符合哪些协议。类型继承子句以冒号 (:) 开头,后跟一系列类型标识符。
506
533
@@ -511,8 +538,11 @@ if let first = mixed.first as? String {
511
538
对于为枚举案例分配原始值的枚举定义,类型继承子句可以是指定这些原始值类型的单个命名类型。有关使用类型继承子句指定其原始值类型的枚举定义示例,请参阅 < doc:Enumerations#原始值 > 。
512
539
513
540
> 类型继承子句的语法:
514
- > > * type-inheritance-clause* → ** ` : ` ** * type-inheritance-list*
515
- > > * type-inheritance-list* → * attributes* _ ?_ * type-identifier* | * attributes* _ ?_ * type-identifier* ** ` , ` ** * type-inheritance-list*
541
+ >
542
+ > * type-inheritance-clause* → ** ` : ` ** * type-inheritance-list*
543
+ > * type-inheritance-list* → * attributes* _ ?_ * type-identifier* | * attributes* _ ?_ * type-identifier* ** ` , ` ** * type-inheritance-list*
544
+
545
+ ## 类型推断
516
546
517
547
Swift 广泛使用 * 类型推断* ,允许你在代码中省略许多变量和表达式的类型或部分类型。
518
548
0 commit comments