remove extra >
This commit is contained in:
@ -430,7 +430,6 @@ struct Point {
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
{ (parameters) -> return type in
|
{ (parameters) -> return type in
|
||||||
>
|
|
||||||
statements
|
statements
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -448,7 +447,6 @@ struct Point {
|
|||||||
```swift
|
```swift
|
||||||
myFunction {
|
myFunction {
|
||||||
(x: Int, y: Int) -> Int in
|
(x: Int, y: Int) -> Int in
|
||||||
>
|
|
||||||
return x + y
|
return x + y
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -729,7 +727,6 @@ let myGreeting = greetings[keyPath: \[String].[1]]
|
|||||||
var index = 2
|
var index = 2
|
||||||
let path = \[String].[index]
|
let path = \[String].[index]
|
||||||
let fn: ([String]) -> String = { strings in strings[index] }
|
let fn: ([String]) -> String = { strings in strings[index] }
|
||||||
>
|
|
||||||
|
|
||||||
print(greetings[keyPath: path])
|
print(greetings[keyPath: path])
|
||||||
// 打印 "bonjour"
|
// 打印 "bonjour"
|
||||||
@ -840,7 +837,6 @@ extension SomeClass {
|
|||||||
func doSomething(_ x: String) { }
|
func doSomething(_ x: String) { }
|
||||||
}
|
}
|
||||||
let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (String) -> Void)
|
let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (String) -> Void)
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
由于选择器是在编译时创建的,因此编译器可以检查方法或者属性是否存在,以及是否在运行时暴露给了 Objective-C 。
|
由于选择器是在编译时创建的,因此编译器可以检查方法或者属性是否存在,以及是否在运行时暴露给了 Objective-C 。
|
||||||
@ -1036,7 +1032,6 @@ class SomeSubClass: SomeSuperClass {
|
|||||||
```swift
|
```swift
|
||||||
// 类型注解是必须的,因为 String 类型有多种构造器
|
// 类型注解是必须的,因为 String 类型有多种构造器
|
||||||
let initializer: Int -> String = String.init
|
let initializer: Int -> String = String.init
|
||||||
>
|
|
||||||
let oneTwoThree = [1, 2, 3].map(initializer).reduce("", combine: +)
|
let oneTwoThree = [1, 2, 3].map(initializer).reduce("", combine: +)
|
||||||
print(oneTwoThree)
|
print(oneTwoThree)
|
||||||
// 打印“123”
|
// 打印“123”
|
||||||
@ -1106,7 +1101,6 @@ let b = instance.someMethod(_:y:) // 无歧义
|
|||||||
let d = instance.overloadedMethod // 有歧义
|
let d = instance.overloadedMethod // 有歧义
|
||||||
let d = instance.overloadedMethod(_:y:) // 有歧义
|
let d = instance.overloadedMethod(_:y:) // 有歧义
|
||||||
let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义
|
let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
如果点号(`.`)出现在行首,它会被视为显式成员表达式的一部分,而不是隐式成员表达式的一部分。例如如下代码所展示的被分为多行的链式方法调用:
|
如果点号(`.`)出现在行首,它会被视为显式成员表达式的一部分,而不是隐式成员表达式的一部分。例如如下代码所展示的被分为多行的链式方法调用:
|
||||||
@ -1115,7 +1109,6 @@ let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义
|
|||||||
let x = [10, 3, 20, 15, 4]
|
let x = [10, 3, 20, 15, 4]
|
||||||
.sort()
|
.sort()
|
||||||
.filter { $0 > 5 }
|
.filter { $0 > 5 }
|
||||||
>
|
|
||||||
.map { $0 * 100 }
|
.map { $0 * 100 }
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1232,7 +1225,6 @@ if let unwrappedC = c {
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func someFunctionWithSideEffects() -> Int {
|
func someFunctionWithSideEffects() -> Int {
|
||||||
>
|
|
||||||
// 译者注:为了能看出此函数是否被执行,加上了一句打印
|
// 译者注:为了能看出此函数是否被执行,加上了一句打印
|
||||||
print("someFunctionWithSideEffects")
|
print("someFunctionWithSideEffects")
|
||||||
return 42
|
return 42
|
||||||
|
|||||||
@ -351,20 +351,16 @@ typealias 类型别名 = 现存类型
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
typealias StringDictionary<Value> = Dictionary<String, Value>
|
typealias StringDictionary<Value> = Dictionary<String, Value>
|
||||||
>
|
|
||||||
|
|
||||||
// 下列两个字典拥有同样的类型
|
// 下列两个字典拥有同样的类型
|
||||||
var dictionary1: StringDictionary<Int> = [:]
|
var dictionary1: StringDictionary<Int> = [:]
|
||||||
>
|
|
||||||
var dictionary2: Dictionary<String, Int> = [:]
|
var dictionary2: Dictionary<String, Int> = [:]
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
当一个类型别名带着泛型参数一起被声明时,这些参数的约束必须与现有参数的约束完全匹配。例如:
|
当一个类型别名带着泛型参数一起被声明时,这些参数的约束必须与现有参数的约束完全匹配。例如:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
typealias DictionaryOfInts<Key: Hashable> = Dictionary<Key, Int>
|
typealias DictionaryOfInts<Key: Hashable> = Dictionary<Key, Int>
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
因为类型别名可以和现有类型相互交换使用,类型别名不可以引入额外的类型约束。
|
因为类型别名可以和现有类型相互交换使用,类型别名不可以引入额外的类型约束。
|
||||||
@ -384,7 +380,6 @@ protocol Sequence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||||
>
|
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -452,7 +447,6 @@ func 函数名称(参数列表) {
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func f(x: Int, y: Int) -> Int { return x + y }
|
func f(x: Int, y: Int) -> Int { return x + y }
|
||||||
>
|
|
||||||
f(x: 1, y: 2) // 参数 x 和 y 都有标签
|
f(x: 1, y: 2) // 参数 x 和 y 都有标签
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -491,7 +485,6 @@ repeatGreeting("Hello, world!", count: 2) // count 有标签, greeting 没有
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func someFunction(a: inout Int) -> () -> Int {
|
func someFunction(a: inout Int) -> () -> Int {
|
||||||
>
|
|
||||||
return { [a] in return a + 1 }
|
return { [a] in return a + 1 }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -531,7 +524,7 @@ _ : 参数类型
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func f(x: Int = 42) -> Int { return x }
|
func f(x: Int = 42) -> Int { return x }
|
||||||
>
|
|
||||||
f() // 有效,使用默认值
|
f() // 有效,使用默认值
|
||||||
f(7) // 有效,提供了值
|
f(7) // 有效,提供了值
|
||||||
f(x: 7) // 无效,该参数没有外部名称
|
f(x: 7) // 无效,该参数没有外部名称
|
||||||
@ -549,7 +542,6 @@ f(x: 7) // 无效,该参数没有外部名称
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func 函数名称(参数列表) throws -> 返回类型 {
|
func 函数名称(参数列表) throws -> 返回类型 {
|
||||||
>
|
|
||||||
语句
|
语句
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -567,7 +559,6 @@ func 函数名称(参数列表) throws -> 返回类型 {
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func someFunction(callback: () throws -> Void) rethrows {
|
func someFunction(callback: () throws -> Void) rethrows {
|
||||||
>
|
|
||||||
try callback()
|
try callback()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -579,7 +570,6 @@ func alwaysThrows() throws {
|
|||||||
throw SomeError.error
|
throw SomeError.error
|
||||||
}
|
}
|
||||||
func someFunction(callback: () throws -> Void) rethrows {
|
func someFunction(callback: () throws -> Void) rethrows {
|
||||||
>
|
|
||||||
do {
|
do {
|
||||||
try callback()
|
try callback()
|
||||||
try alwaysThrows() // 非法, alwaysThrows() 不是一个抛出函数类型的参数
|
try alwaysThrows() // 非法, alwaysThrows() 不是一个抛出函数类型的参数
|
||||||
@ -707,7 +697,6 @@ enum Number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// f 的类型为 (Int) -> Number
|
// f 的类型为 (Int) -> Number
|
||||||
>
|
|
||||||
let f = Number.integer
|
let f = Number.integer
|
||||||
|
|
||||||
// 利用 f 把一个整数数组转成 Number 数组
|
// 利用 f 把一个整数数组转成 Number 数组
|
||||||
@ -723,7 +712,6 @@ let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
enum Tree<T> {
|
enum Tree<T> {
|
||||||
>
|
|
||||||
case empty
|
case empty
|
||||||
indirect case node(value: T, left: Tree, right:Tree)
|
indirect case node(value: T, left: Tree, right:Tree)
|
||||||
}
|
}
|
||||||
@ -1148,7 +1136,6 @@ var 属性名: 类型 { get set }
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
subscript (参数列表) -> 返回类型 { get set }
|
subscript (参数列表) -> 返回类型 { get set }
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
下标声明只为符合类型声明了 getter 和 setter 要求。如果下标声明包含 `get` 和 `set` 关键字,符合类型也必须实现 getter 和 setter 子句。如果下标声明只包含 `get` 关键字,符合类型必须实现 getter 子句,可以选择是否实现 setter 子句。
|
下标声明只为符合类型声明了 getter 和 setter 要求。如果下标声明包含 `get` 和 `set` 关键字,符合类型也必须实现 getter 和 setter 子句。如果下标声明只包含 `get` 关键字,符合类型必须实现 getter 子句,可以选择是否实现 setter 子句。
|
||||||
@ -1441,18 +1428,15 @@ doSomething(with: oneAndTwo)
|
|||||||
```swift
|
```swift
|
||||||
protocol Serializable {
|
protocol Serializable {
|
||||||
func serialize() -> Any
|
func serialize() -> Any
|
||||||
>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Array: Serializable where Element == Int {
|
extension Array: Serializable where Element == Int {
|
||||||
func serialize() -> Any {
|
func serialize() -> Any {
|
||||||
>
|
|
||||||
// implementation
|
// implementation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
extension Array: Serializable where Element == String {
|
extension Array: Serializable where Element == String {
|
||||||
func serialize() -> Any {
|
func serialize() -> Any {
|
||||||
>
|
|
||||||
// implementation
|
// implementation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1468,7 +1452,6 @@ extension String: SerializableInArray { }
|
|||||||
|
|
||||||
extension Array: Serializable where Element: SerializableInArray {
|
extension Array: Serializable where Element: SerializableInArray {
|
||||||
func serialize() -> Any {
|
func serialize() -> Any {
|
||||||
>
|
|
||||||
// 具体实现
|
// 具体实现
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1536,7 +1519,6 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
subscript (参数列表) -> 返回类型 {
|
subscript (参数列表) -> 返回类型 {
|
||||||
>
|
|
||||||
get {
|
get {
|
||||||
语句
|
语句
|
||||||
}
|
}
|
||||||
|
|||||||
@ -151,7 +151,6 @@ dial.dynamicallyCall(withArguments: [4, 1, 1])
|
|||||||
@dynamicCallable
|
@dynamicCallable
|
||||||
struct Repeater {
|
struct Repeater {
|
||||||
func dynamicallyCall(withKeywordArguments pairs: KeyValuePairs<String, Int>) -> String {
|
func dynamicallyCall(withKeywordArguments pairs: KeyValuePairs<String, Int>) -> String {
|
||||||
>
|
|
||||||
return pairs
|
return pairs
|
||||||
.map { label, count in
|
.map { label, count in
|
||||||
repeatElement(label, count: count).joined(separator: " ")
|
repeatElement(label, count: count).joined(separator: " ")
|
||||||
@ -195,7 +194,6 @@ struct DynamicStruct {
|
|||||||
let dictionary = ["someDynamicMember": 325,
|
let dictionary = ["someDynamicMember": 325,
|
||||||
"someOtherMember": 787]
|
"someOtherMember": 787]
|
||||||
subscript(dynamicMember member: String) -> Int {
|
subscript(dynamicMember member: String) -> Int {
|
||||||
>
|
|
||||||
return dictionary[member] ?? 1054
|
return dictionary[member] ?? 1054
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -228,7 +228,6 @@ default:
|
|||||||
```swift
|
```swift
|
||||||
// 重载 ~= 运算符对字符串和整数进行比较
|
// 重载 ~= 运算符对字符串和整数进行比较
|
||||||
func ~=(pattern: String, value: Int) -> Bool {
|
func ~=(pattern: String, value: Int) -> Bool {
|
||||||
>
|
|
||||||
return pattern == "\(value)"
|
return pattern == "\(value)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
func simpleMax<T: Comparable>(_ x: T, _ y: T) -> T {
|
func simpleMax<T: Comparable>(_ x: T, _ y: T) -> T {
|
||||||
>
|
|
||||||
if x < y {
|
if x < y {
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
@ -117,7 +116,6 @@ struct Dictionary<Key: Hashable, Value>: CollectionType, DictionaryLiteralConver
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||||
>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
|
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
|
||||||
|
|||||||
Reference in New Issue
Block a user