提交 e7201e69 编写于 作者: W wizardforcel

2020-07-07 17:41:44

上级 aec99201
......@@ -130,7 +130,7 @@ x -= 2
print(x)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
8
......@@ -167,7 +167,7 @@ print(5 % 2 ) //remainder operator
print("I love " + "Swift") //operator can also be used to concatenate string
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
30
......@@ -189,7 +189,7 @@ print(x)
print(y)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
2
......@@ -221,7 +221,7 @@ print(msg == "Hello")
print(msg != "Hello")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......@@ -237,7 +237,7 @@ print(5 >= 5)
print(5 <= 4)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
false
......@@ -267,7 +267,7 @@ print(true && false)
print(false || true)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......
......@@ -93,7 +93,7 @@ if number > 0 {
print("This statement is always executed.")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Number is positive.
......@@ -163,7 +163,7 @@ else {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Number is 0.
......
......@@ -115,7 +115,7 @@ for _ in 1..<3 {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello world!
......@@ -148,7 +148,7 @@ for i in stride(from: 1, to: 10, by: interval) {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
1
......@@ -200,7 +200,7 @@ for language in programmingLanguages {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Swift
......@@ -229,7 +229,7 @@ for value in "I♥Swift!" {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
I
......@@ -257,7 +257,7 @@ for (index, language) in programmingLanguages.enumerated() {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
0:Swift
......@@ -284,7 +284,7 @@ for value in "I♥Swift!" where value != "!" {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
I
......
......@@ -134,7 +134,7 @@ repeat {
print("outside of repeat while loop")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
You have passed level 0
......@@ -187,7 +187,7 @@ repeat {
} while (true)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!
......
......@@ -159,7 +159,7 @@ for i in 1...rows {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
1
......
......@@ -145,7 +145,7 @@ outerloop: for j in 1...2{
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
i = 1
......
......@@ -144,7 +144,7 @@ outerloop: for j in 1...2 {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
i = 1
......
......@@ -59,7 +59,7 @@ let someIntArr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(someIntArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[1, 2, 3, 4, 5, 6, 7, 8, 9]
......@@ -78,7 +78,7 @@ let arrWithRepeatingValues = Array(repeating: "Hello, World", count: 4)
print(arrWithRepeatingValues)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["Hello, World", "Hello, World", "Hello, World", "Hello, World"]
......@@ -124,7 +124,7 @@ print(intArr[2])
print(intArr[3])
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
21
......@@ -152,7 +152,7 @@ intArr[3] = 21
print(intArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[12, 42, 45, 21]
......@@ -170,7 +170,7 @@ intArr = [1,2,3]
print(intArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[1, 2, 3]
......@@ -187,7 +187,7 @@ var intArr = [21, 34, 54, 12]
intArr[4] = 10
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
fatal error: Index out of range
......@@ -212,7 +212,7 @@ let intArr = [21, 34, 54, 12]
print(intArr.isEmpty)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
false
......@@ -231,7 +231,7 @@ let intArr = [21, 34, 54, 12]
print(intArr.first)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional(21)
......@@ -253,7 +253,7 @@ intArr.append(32)
print(intArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[21, 34, 54, 12, 32]
......@@ -268,7 +268,7 @@ firstArr.append(contentsOf: secondArr)
print(firstArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[1, 2, 3, 4, 5, 6, 7, 8]
......@@ -288,7 +288,7 @@ intArr.insert(22, at: 1)
print(intArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[21, 22, 34, 54, 12]
......@@ -311,7 +311,7 @@ print("removed value is \(removedVal)")
print(strArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
removed value is bc
......@@ -334,7 +334,7 @@ let reversedArr = Array(intArr.reversed())
print(reversedArr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[24, 23, 22, 21]
......@@ -353,7 +353,7 @@ let floatArr = [10.2,21.3,32.0,41.3]
print(floatArr.count)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
4
......@@ -370,7 +370,7 @@ let intArr = [21, 34, 54, 12]
print(intArr[-1])
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
fatal error: Index out of range
......@@ -389,7 +389,7 @@ if let index = intArr.index(of: 34) {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
found index
......
......@@ -59,7 +59,7 @@ let someIntSet:Set = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(someIntSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[2, 4, 9, 5, 6, 7, 3, 1, 8]
......@@ -78,7 +78,7 @@ let someStrSet:Set = ["ab","bc","cd","de","ab"]
print(someStrSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["de", "ab", "cd", "bc"]
......@@ -105,7 +105,7 @@ for val in someStrSet {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
de
......@@ -129,7 +129,7 @@ print(someVal)
print(someStrSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional("cd")
......@@ -152,7 +152,7 @@ if let someVal = someStrSet.remove("cd") {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
cd
......@@ -173,7 +173,7 @@ someStrSet.insert("ef")
print(someStrSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["ab", "de", "cd", "ef", "bc"]
......@@ -223,13 +223,13 @@ print(a.intersection(b))
[7, 3]
```
因此,`print(a.intersection(b))`将输出一个新的集合,其值在 a 和 b 中都是相同的 **[7,3]**
因此,`print(a.intersection(b))`将输出一个新的集合,其值在`a``b`中都是相同的`[7, 3]`
* * *
### 3.减去
### 3.差集
两个集合 a 和 b 的减法是包含 a 的所有元素的集合,但删除了也属于 b 的元素。
两个集合`a``b`的减法是包含`a`的所有元素的集合,但删除了也属于`b`的元素。
```swift
let a: Set = [1, 3, 5, 7, 9]
......@@ -243,13 +243,13 @@ print(a.subtracting(b))
[5, 9, 1]
```
因此,`print(a.subtracting(b))`输出具有值 **[5、9、1]** 的新集合。
因此,`print(a.subtracting(b))`输出具有值`[5, 9, 1]`的新集合。
* * *
### 4.对称差
### 4.对称差
两个集合 a 和 b 的对称差是包含所有元素的集合,这些元素位于两个集合中的任何一个中,但不在两个集合中。
两个集合`a``b`的对称差是包含所有元素的集合,这些元素位于两个集合中的任何一个中,但不在两个集合中。
```swift
let a: Set = [1, 3, 5, 7, 9]
......@@ -263,7 +263,7 @@ print(a.symmetricDifference(b))
[5, 6, 8, 0, 1, 9]
```
因此,`print(a.symmetricDifference(b))`输出具有值 **[5、6、8、0、1、9]** 的新集合。
因此,`print(a.symmetricDifference(b))`输出具有值`[5, 6, 8, 0, 1, 9]`的新集合。
* * *
......@@ -271,7 +271,7 @@ print(a.symmetricDifference(b))
### 设置相等
您可以使用`==`运算符检查两组是否包含相同的元素。 如果两个集合包含相同的元素,则返回 true,否则返回 false
您可以使用`==`运算符检查两组是否包含相同的元素。 如果两个集合包含相同的元素,则返回`true`,否则返回`false`
#### 示例 5:设置相等操作
......@@ -332,29 +332,29 @@ isStrictSubset: true
isDisjointWith: false
```
让我们分析下面的 print 语句中使用的方法:
让我们分析下面的`print`语句中使用的方法:
* `isSubset`返回`true`,因为集合 b 包含 a 中的所有元素
* `isSuperset`返回`true`,因为 b 包含 a 的所有值。
* `isStrictSubset`返回`true`,因为集合 b 包含 a 中的所有元素,并且两个集合都不相等。
* `isDisjointWith`返回`false`,因为 a 和 b 具有一些共同的值。
* `isSubset`返回`true`,因为集合`b`包含`a`中的所有元素
* `isSuperset`返回`true`,因为`b`包含`a`的所有值。
* `isStrictSubset`返回`true`,因为集合`b`包含`a`中的所有元素,并且两个集合都不相等。
* `isDisjointWith`返回`false`,因为`a``b`具有一些共同的值。
* * *
## 内置的一些有用的 Set 函数&属性
## 内置的一些有用的`Set`函数&属性
### 1\. isEmpty
### 1\. `isEmpty`
此属性确定集合是否为空。 如果集合不包含任何值,则返回`true`,否则返回`false`
#### 示例 7:isEmpty 如何工作?
#### 示例 7:`isEmpty`如何工作?
```swift
let intSet:Set = [21, 34, 54, 12]
print(intSet.isEmpty)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
false
......@@ -373,13 +373,13 @@ let intSet = [21, 34, 54, 12]
print(intSet.first)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional(54)
```
由于 set 是无序集合,因此 first 属性不能保证 set 的第一个元素。 您可能会获得 54 以外的其他值。
由于集是无序集合,因此`first`属性不能保证集的第一个元素。 您可能会获得 54 以外的其他值。
同样,您可以使用`last`属性访问集合的最后一个元素。
......@@ -397,7 +397,7 @@ intSet.insert(50)
print(intSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[54, 12, 50, 21, 34]
......@@ -409,7 +409,7 @@ When you run the program, the output will be:
此函数以相反的顺序返回集合的元素。
#### 示例 10:reversed()如何工作?
#### 示例 10:`reversed()`如何工作?
```swift
var intSet:Set = [21, 22, 23, 24, 25]
......@@ -418,7 +418,7 @@ let reversedSet = intSet.reversed()
print(reversedSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
[22, 23, 21, 24, 25]
......@@ -438,7 +438,7 @@ let floatSet:Set = [10.2, 21.3, 32.0, 41.3]
print(floatSet.count)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
4
......@@ -450,7 +450,7 @@ When you run the program, the output will be:
此函数从集合中删除并返回第一个值。
#### 示例 12:removeFirst 如何工作?
#### 示例 12:`removeFirst`如何工作?
```swift
var strSet:Set = ["ab", "bc", "cd", "de"]
......@@ -459,7 +459,7 @@ print("removed value is \(removedVal)")
print(strSet)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
removed value is de
......
......@@ -59,7 +59,7 @@ let someDic = ["a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9]
print(someDic)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["b": 2, "a": 1, "i": 9, "c": 3, "e": 5, "f": 6, "g": 7, "d": 4, "h": 8]
......@@ -82,7 +82,7 @@ let newDictionary = Dictionary(uniqueKeysWithValues: zip(customKeys,customValues
print(newDictionary)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["Amazon": "Jeff", "Google": "Larry", "Facebook": "Mark"]
......@@ -106,7 +106,7 @@ print(someDic["a"])
print(someDic["h"])
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional(1)
......@@ -126,7 +126,7 @@ for (key,value) in someDic {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
key:b value:2
......@@ -152,7 +152,7 @@ someDictionary["Japan"] = "Tokyo"
print(someDictionary)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["Japan": "Tokyo", "China": "Beijing", "India": "NewDelhi", "Nepal": "Kathmandu"]
......@@ -172,7 +172,7 @@ someDictionary["Nepal"] = "KATHMANDU"
print(someDictionary)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["China": "Beijing", "India": "NewDelhi", "Nepal": "KATHMANDU"]
......@@ -193,7 +193,7 @@ let someDictionary = ["Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi"
print(someDictionary.isEmpty)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
false
......@@ -212,7 +212,7 @@ let someDictionary = ["Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi"
print(someDictionary.first)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional((key: "China", value: "Beijing"))
......@@ -231,7 +231,7 @@ let someDictionary = ["Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi"
print(someDictionary.count)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
3
......@@ -251,7 +251,7 @@ let dictKeys = Array(someDictionary.keys)
print(dictKeys)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
["China", "India", "Nepal"]
......@@ -274,7 +274,7 @@ print(val)
print(someDictionary)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional("Kathmandu")
......@@ -297,7 +297,7 @@ let val = someDictionary["Japan"]
print(val)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
nil
......@@ -315,7 +315,7 @@ let val = someDictionary["nepal"]
print(val)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
nil
......@@ -333,7 +333,7 @@ let val = someDictionary["nepal", default:"Not Found"]
print(val)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Not Found
......
......@@ -74,7 +74,7 @@ let result = operation(2, 3)
print(result)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
5
......
......@@ -67,7 +67,7 @@ for value in 1..<3 {
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
1
......@@ -119,7 +119,7 @@ print(range.contains(1))
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......
......@@ -70,7 +70,7 @@ var siteName = "Apple.com"
print(siteName)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Apple.com
......@@ -90,7 +90,7 @@ siteName = "Programiz.com"
print(siteName)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Programiz.com
......@@ -143,7 +143,7 @@ You can also assign the value inline as
let siteName:String = "Apple.com"
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Apple.com
......@@ -162,7 +162,7 @@ let siteName = "Apple.com"
print(siteName)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Apple.com
......@@ -225,7 +225,7 @@ print(binaryNumber)
print(1231)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
255
......@@ -270,7 +270,7 @@ print(someFloat)
print(someAnotherFloat)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
12.23
......@@ -292,7 +292,7 @@ print(someFloat)
print(someAnotherFloat)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
15360.0
......
......@@ -106,7 +106,7 @@ highScore = -100
print(highScore)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
100
......@@ -148,7 +148,7 @@ print(Int8.min)
print(Int8.max)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
-128
......@@ -190,7 +190,7 @@ let highScore:Float = 100.232
print(highScore)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
100.232
......@@ -212,7 +212,7 @@ let highScore:Double = 100.232321212121
print(highScore)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
100.232321212121
......@@ -234,7 +234,7 @@ print(playerName)
print(playerNameWithUnicode)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
J
......@@ -270,7 +270,7 @@ print(playerNameWithQuotes)
print(playerNameWithUnicode)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Jack
......
......@@ -39,7 +39,7 @@ print(someValue)
print(someAnotherValue)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
nil
......@@ -60,7 +60,7 @@ print(someValue)
print(someValue!)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Optional(5)
......@@ -86,7 +86,7 @@ let someValue:Int! = 5
print(someValue)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
5
......@@ -144,7 +144,7 @@ if someAnotherValue != nil {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
doesn't contain value
......@@ -178,7 +178,7 @@ if let temp = someAnotherValue {
}
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
doesn't contain value
......@@ -207,7 +207,7 @@ func testFunction() {
testFunction()
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
It has some value 5
......@@ -230,7 +230,7 @@ let unwrappedValue:Int = someValue ?? defaultValue
print(unwrappedValue)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
5
......@@ -245,7 +245,7 @@ let unwrappedValue:Int = someValue ?? defaultValue
print(unwrappedValue)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
10
......
......@@ -70,7 +70,7 @@ let heartShape:Character = "\u{2665}"
print(heartShape)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
......@@ -103,7 +103,7 @@ print(someString)
print(someMessage)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, world!
......@@ -132,7 +132,7 @@ print(someString == someMessage)
print(someString == someAnotherMessage)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
false
......@@ -156,7 +156,7 @@ result.append("!")
print(result)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World
......@@ -181,7 +181,7 @@ helloStr += worldStr
print(helloStr)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!
......@@ -224,7 +224,7 @@ let congratsMessage = "Congratulations \(playerName)!. Your highest score is \(p
print(congratsMessage)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Congratulations Jack!. Your highest score is 99.
......@@ -245,7 +245,7 @@ var emptyString = ""
print(emptyString.isEmpty)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......@@ -264,7 +264,7 @@ let someString = "hello, world!"
print(someString.capitalized)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!
......@@ -284,7 +284,7 @@ print(someString.uppercased())
print(someString.lowercased())
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
HELLO, WORLD!
......@@ -304,7 +304,7 @@ let someString = "Hello, World!"
print(someString.count)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
13
......@@ -324,7 +324,7 @@ print(someString.hasPrefix("Hell"))
print(someString.hasPrefix("hell"))
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......@@ -344,7 +344,7 @@ print(someString.hasSuffix("rld!"))
print(someString.hasSuffix("Rld!"))
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
true
......
......@@ -48,7 +48,7 @@ print(helloMsg)
print(123.45)
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!
......@@ -71,7 +71,7 @@ print("I love Swift.")
print("I also love Taylor Swift.")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!I love Swift.
......@@ -94,7 +94,7 @@ I also love Taylor Swift.
print("Hello, World!", 2020, "See you soon", separator: ". ")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello, World!. 2020\. See you soon
......@@ -116,7 +116,7 @@ Hello, World!. 2020\. See you soon
print("Hello, \rWorld!")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello,
......@@ -136,7 +136,7 @@ World!
""")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Hello,
......@@ -154,7 +154,7 @@ var helloMsg = "Hello, World!"
print("I have a message \(helloMsg)")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
I have a message Hello, World!
......@@ -180,7 +180,7 @@ let name = readLine()
print("Your favorite programming language is \(name!).")
```
When you run the program, the output will be:
运行该程序时,输出为:
```swift
Please Enter your favorite programming language.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册