@@ -583,7 +583,7 @@ func <#function name#>(<#parameters#>) {
583
583
584
584
``` swift
585
585
func f (x : Int , y : Int ) -> Int { return x + y }
586
- f (x : 1 , y : 2 ) // both x and y are labeled
586
+ f (x : 1 , y : 2 ) // x 和 y 都带有标签
587
587
```
588
588
589
589
<!--
@@ -592,7 +592,7 @@ f(x: 1, y: 2) // both x and y are labeled
592
592
```swifttest
593
593
-> func f(x: Int, y: Int) -> Int { return x + y }
594
594
>> let r0 =
595
- -> f(x: 1, y: 2) // both x and y are labeled
595
+ -> f(x: 1, y: 2) // x 和 y 都带有标签
596
596
>> assert(r0 == 3)
597
597
```
598
598
-->
@@ -614,8 +614,8 @@ _ <#parameter name#>: <#parameter type#>
614
614
在参数名称前加下划线(` _ ` )可以抑制参数标签。相应的参数在函数或方法调用中必须没有标签。
615
615
616
616
``` swift
617
- func repeatGreeting (_ greeting : String , count n : Int ) { /* Greet n times */ }
618
- repeatGreeting (" Hello, world!" , count : 2 ) // count is labeled, greeting is not
617
+ func repeatGreeting (_ greeting : String , count n : Int ) { /* 打招呼 n 次 */ }
618
+ repeatGreeting (" Hello, world!" , count : 2 ) // count 带有标签, greeting 没有
619
619
```
620
620
621
621
<!--
@@ -656,7 +656,7 @@ func someFunction(a: inout Int) {
656
656
``` swift
657
657
var x = 7
658
658
someFunction (& x)
659
- print (x) // Prints "8"
659
+ print (x) // 打印 "8"
660
660
```
661
661
662
662
输入输出参数的传递方式如下:
@@ -677,7 +677,7 @@ func someFunction(a: inout Int) {
677
677
a += someValue
678
678
}
679
679
680
- // Error: This causes a runtime exclusivity violation
680
+ // 错误:这会导致运行时排他性违规
681
681
someFunction (& someValue)
682
682
```
683
683
@@ -690,7 +690,7 @@ func someFunction(a: inout Int, b: inout Int) {
690
690
b += 1
691
691
}
692
692
693
- // Error: Cannot pass the same value to multiple in-out parameters
693
+ // 错误:不能将同一个值传递给多个 in-out 参数
694
694
someFunction (& someValue, & someValue)
695
695
```
696
696
@@ -733,11 +733,11 @@ func someFunction(a: inout Int) -> () -> Int {
733
733
734
734
``` swift
735
735
func multithreadedFunction (queue : DispatchQueue, x : inout Int ) {
736
- // Make a local copy and manually copy it back.
736
+ // 创建一个本地副本,并在函数结束时手动将其复制回去。
737
737
var localX = x
738
738
defer { x = localX }
739
739
740
- // Operate on localX asynchronously, then wait before returning.
740
+ // 异步操作 localX,然后在返回之前等待。
741
741
queue.async { someMutatingOperation (& localX) }
742
742
queue.sync {}
743
743
}
@@ -813,7 +813,7 @@ Where are copies implicitly inserted?
813
813
` borrowing ` 修饰符表示该函数不保留参数的值。在这种情况下,调用者保持对象的所有权,并对对象的生命周期负责。使用 ` borrowing ` 可以在函数仅暂时使用对象时最小化开销。
814
814
815
815
``` swift
816
- // `isLessThan` does not keep either argument
816
+ // `isLessThan` 不会保留任一参数
817
817
func isLessThan (lhs : borrowing A, rhs : borrowing A) -> Bool {
818
818
...
819
819
}
@@ -822,7 +822,7 @@ func isLessThan(lhs: borrowing A, rhs: borrowing A) -> Bool {
822
822
如果函数需要保持参数的值,例如,通过将其存储在全局变量中——你可以使用 ` copy ` 明确地复制该值。
823
823
824
824
``` swift
825
- // As above, but this `isLessThan` also wants to record the smallest value
825
+ // 如上所述,但这个 `isLessThan` 还需要记录最小值
826
826
func isLessThan (lhs : borrowing A, rhs : borrowing A) -> Bool {
827
827
if lhs < storedValue {
828
828
storedValue = copy lhs
@@ -836,7 +836,7 @@ func isLessThan(lhs: borrowing A, rhs: borrowing A) -> Bool {
836
836
相反,` consuming ` 参数修饰符表示该函数拥有该值的所有权,负责在函数返回之前存储或销毁它。
837
837
838
838
``` swift
839
- // `store` keeps its argument, so mark it `consuming`
839
+ // `store` 会保留它的参数,因此将其标记为 `consuming`
840
840
func store (a : consuming A) {
841
841
someGlobalVariable = a
842
842
}
@@ -845,16 +845,16 @@ func store(a: consuming A) {
845
845
使用 ` consuming ` 可以在调用者在函数调用后不再需要使用该对象时,最小化开销。
846
846
847
847
``` swift
848
- // Usually, this is the last thing you do with a value
848
+ // 通常,这是你对一个值执行的最后一件事
849
849
store (a : value)
850
850
```
851
851
852
852
如果在函数调用后继续使用可复制对象,编译器会在函数调用之前自动复制该对象。
853
853
854
854
``` swift
855
- // The compiler inserts an implicit copy here
856
- store (a : someValue) // This function consumes someValue
857
- print (someValue) // This uses the copy of someValue
855
+ // 编译器会在这里插入一个隐式副本
856
+ store (a : someValue) // 此函数消耗 someValue
857
+ print (someValue) // 这里使用的是 someValue 的副本
858
858
```
859
859
860
860
与 ` inout ` 不同,` borrowing ` 和 ` consuming ` 参数在调用函数时不需要任何特殊标记:
@@ -869,33 +869,31 @@ someFunction(a: someA, b: someB)
869
869
870
870
``` swift
871
871
func borrowingFunction1 (a : borrowing A) {
872
- // Error: Cannot implicitly copy a
873
- // This assignment requires a copy because
874
- // `a` is only borrowed from the caller.
872
+ // 错误:无法隐式复制 a
873
+ // 这个赋值操作需要复制,因为 `a` 只是从调用者那里借来的。
875
874
someGlobalVariable = a
876
875
}
877
876
878
877
func borrowingFunction2 (a : borrowing A) {
879
- // OK: Explicit copying works
878
+ // 可以:显式复制是可以的
880
879
someGlobalVariable = copy a
881
880
}
882
881
883
882
func consumingFunction1 (a : consuming A) {
884
- // Error: Cannot implicitly copy a
885
- // This assignment requires a copy because
886
- // of the following `print`
883
+ // 错误:无法隐式复制 a
884
+ // 这个赋值操作需要复制,因为后面有 `print`
887
885
someGlobalVariable = a
888
886
print (a)
889
887
}
890
888
891
889
func consumingFunction2 (a : consuming A) {
892
- // OK: Explicit copying works regardless
890
+ // 可以:显式复制在这种情况下有效
893
891
someGlobalVariable = copy a
894
892
print (a)
895
893
}
896
894
897
895
func consumingFunction3 (a : consuming A) {
898
- // OK: No copy needed here because this is the last use
896
+ // 可以:不需要复制,因为这是最后一次使用
899
897
someGlobalVariable = a
900
898
}
901
899
```
@@ -925,9 +923,9 @@ _ : <#parameter type#>
925
923
926
924
``` swift
927
925
func f (x : Int = 42 ) -> Int { return x }
928
- f () // Valid, uses default value
929
- f (x : 7 ) // Valid, uses the value provided
930
- f (7 ) // Invalid, missing argument label
926
+ f () // 有效,使用默认值
927
+ f (x : 7 ) // 有效,使用提供的值
928
+ f (7 ) // 无效,缺少参数标签
931
929
```
932
930
933
931
<!--
@@ -1042,7 +1040,7 @@ struct CallableStruct {
1042
1040
let callable = CallableStruct (value : 100 )
1043
1041
callable (4 , scale : 2 )
1044
1042
callable.callAsFunction (4 , scale : 2 )
1045
- // Both function calls print 208.
1043
+ // 两个函数调用都打印 208。
1046
1044
```
1047
1045
1048
1046
<!--
@@ -1069,7 +1067,7 @@ call-as-function 的方法和来自 `dynamicCallable` 特性的方法在将多
1069
1067
定义一个 call-as-function,或者来自 ` dynamicCallable ` 特性的方法,并不允许你在函数调用表达式以外的任何上下文中将该类型的实例用作函数。例如:
1070
1068
1071
1069
``` swift
1072
- let someFunction1: (Int , Int ) -> Void = callable (_:scale: ) // Error
1070
+ let someFunction1: (Int , Int ) -> Void = callable (_:scale: ) // 错误
1073
1071
let someFunction2: (Int , Int ) -> Void = callable.callAsFunction (_:scale: )
1074
1072
```
1075
1073
@@ -1149,7 +1147,7 @@ func alwaysThrows() throws {
1149
1147
func someFunction (callback : () throws -> Void ) rethrows {
1150
1148
do {
1151
1149
try callback ()
1152
- try alwaysThrows () // Invalid, alwaysThrows() isn't a throwing parameter
1150
+ try alwaysThrows () // 无效, alwaysThrows() 不是一个抛出参数
1153
1151
} catch {
1154
1152
throw AnotherError.error
1155
1153
}
@@ -1331,9 +1329,9 @@ enum Number {
1331
1329
case real (Double )
1332
1330
}
1333
1331
let f = Number.integer
1334
- // f is a function of type (Int) -> Number
1332
+ // f 是一个 (Int) -> Number 的函数类型
1335
1333
1336
- // Apply f to create an array of Number instances with integer values
1334
+ // 应用函数 `f` 来创建一个包含整数值的 ` Number` 实例数组
1337
1335
let evenInts: [Number] = [0 , 2 , 4 , 6 ].map (f)
1338
1336
```
1339
1337
@@ -1757,7 +1755,7 @@ enum MyEnum: SomeProtocol {
1757
1755
1758
1756
``` swift
1759
1757
protocol SomeProtocol : AnyObject {
1760
- /* Protocol members go here */
1758
+ /* 协议成员写在这里 */
1761
1759
}
1762
1760
```
1763
1761
@@ -1889,7 +1887,6 @@ getter 和 setter 的要求可以通过符合类型以多种方式满足。如
1889
1887
subscript (<#parameters#>) -> <#return type#> { get set }
1890
1888
```
1891
1889
1892
-
1893
1890
下标声明仅声明符合协议的类型所需的最小 getter 和 setter 实现要求。如果下标声明同时包含 ` get ` 和 ` set ` 关键字,则符合的类型必须实现 getter 和 setter 子句。如果下标声明仅包含 ` get ` 关键字,则符合的类型必须实现* 至少* 一个 getter 子句,并且可以选择性地实现一个 setter 子句。
1894
1891
1895
1892
在协议声明中声明静态下标要求时,使用 ` static ` 声明修饰符标记下标声明。符合该协议的结构体和枚举使用 ` static ` 关键字声明下标,而符合该协议的类则使用 ` static ` 或 ` class ` 关键字声明下标。为结构体、枚举或类添加协议符合性的扩展使用与其扩展的类型相同的关键字。为静态下标要求提供默认实现的扩展使用 ` static ` 关键字。
@@ -1912,11 +1909,11 @@ protocol SomeProtocol {
1912
1909
}
1913
1910
1914
1911
protocol SubProtocolA : SomeProtocol {
1915
- // This syntax produces a warning.
1912
+ // 此语法会产生警告。
1916
1913
associatedtype SomeType : Equatable
1917
1914
}
1918
1915
1919
- // This syntax is preferred.
1916
+ // 推荐使用此语法。
1920
1917
protocol SubProtocolB : SomeProtocol where SomeType: Equatable { }
1921
1918
```
1922
1919
@@ -2072,10 +2069,10 @@ convenience init(<#parameters#>) {
2072
2069
``` swift
2073
2070
struct SomeStruct {
2074
2071
let property: String
2075
- // produces an optional instance of ' SomeStruct'
2072
+ // 生成一个可选的 ` SomeStruct` 实例
2076
2073
init? (input : String ) {
2077
2074
if input.isEmpty {
2078
- // discard 'self' and return 'nil'
2075
+ // 丢弃 'self' 并返回 'nil'
2079
2076
return nil
2080
2077
}
2081
2078
property = input
@@ -2300,7 +2297,7 @@ extension String: TitledLoggable {
2300
2297
``` swift
2301
2298
let oneAndTwo = Pair (first : " one" , second : " two" )
2302
2299
oneAndTwo.log ()
2303
- // Prints "Pair of 'String': (one, two)"
2300
+ // 打印
2304
2301
```
2305
2302
2306
2303
<!--
@@ -2320,7 +2317,7 @@ func doSomething<T: Loggable>(with x: T) {
2320
2317
x.log ()
2321
2318
}
2322
2319
doSomething (with : oneAndTwo)
2323
- // Prints "(one, two)"
2320
+ // 打印 "(one, two)"
2324
2321
```
2325
2322
2326
2323
<!--
@@ -2352,15 +2349,15 @@ protocol Serializable {
2352
2349
2353
2350
extension Array : Serializable where Element == Int {
2354
2351
func serialize () -> Any {
2355
- // implementation
2352
+ // 实现
2356
2353
}
2357
2354
}
2358
2355
extension Array : Serializable where Element == String {
2359
2356
func serialize () -> Any {
2360
- // implementation
2357
+ // 实现
2361
2358
}
2362
2359
}
2363
- // Error: redundant conformance of 'Array<Element>' to protocol 'Serializable'
2360
+ // 错误: 'Array<Element>' 对协议 'Serializable' 的遵循是多余的
2364
2361
```
2365
2362
2366
2363
<!--
@@ -2402,7 +2399,7 @@ extension String: SerializableInArray { }
2402
2399
2403
2400
extension Array : Serializable where Element : SerializableInArray {
2404
2401
func serialize () -> Any {
2405
- // implementation
2402
+ // 实现
2406
2403
}
2407
2404
}
2408
2405
```
@@ -2484,7 +2481,7 @@ extension Array: MarkedLoggable where Element: MarkedLoggable { }
2484
2481
``` swift
2485
2482
extension Array : Loggable where Element : TitledLoggable { }
2486
2483
extension Array : Loggable where Element : MarkedLoggable { }
2487
- // Error: redundant conformance of 'Array<Element>' to protocol 'Loggable'
2484
+ // 错误: 'Array<Element>' 对协议 'Loggable' 的遵循是多余的
2488
2485
```
2489
2486
2490
2487
<!--
@@ -2839,11 +2836,11 @@ Swift 提供五种访问控制级别:open、public、internal、file private
2839
2836
>
2840
2837
> * actor-isolation-modifier* → ** ` nonisolated ` **
2841
2838
2842
- > Beta 软件 :
2839
+ > 测试版软件 :
2843
2840
>
2844
2841
> 本文件包含有关正在开发的 API 或技术的初步信息。此信息可能会更改,按照本文件实施的软件应与最终操作系统软件进行测试。
2845
2842
>
2846
- > 了解有关使用 [ Apple Beta 软件 ] ( https://developer.apple.com/support/beta-software/ ) 的更多信息。
2843
+ > 了解有关使用 [ Apple 测试版软件 ] ( https://developer.apple.com/support/beta-software/ ) 的更多信息。
2847
2844
2848
2845
<!--
2849
2846
This source file is part of the Swift.org open source project
0 commit comments