替换println 为 print
This commit is contained in:
@ -121,28 +121,28 @@ languageName = "Swift++"
|
||||
|
||||
### 输出常量和变量
|
||||
|
||||
你可以用`println`函数来输出当前常量或变量的值:
|
||||
你可以用`print`函数来输出当前常量或变量的值:
|
||||
|
||||
```swift
|
||||
println(friendlyWelcome)
|
||||
print(friendlyWelcome)
|
||||
// 输出 "Bonjour!"
|
||||
```
|
||||
|
||||
`println`是一个用来输出的全局函数,输出的内容会在最后换行。如果你用 Xcode,`println`将会输出内容到“console”面板上。(另一种函数叫`print`,唯一区别是在输出内容最后不会换行。)
|
||||
`print`是一个用来输出的全局函数,输出的内容会在最后换行。如果你用 Xcode,`print`将会输出内容到“console”面板上。(另一种函数叫`print`,唯一区别是在输出内容最后不会换行。)
|
||||
|
||||
`println`函数输出传入的`String`值:
|
||||
`print`函数输出传入的`String`值:
|
||||
|
||||
```swift
|
||||
println("This is a string")
|
||||
print("This is a string")
|
||||
// 输出 "This is a string"
|
||||
```
|
||||
|
||||
与 Cocoa 里的`NSLog`函数类似的是,`println`函数可以输出更复杂的信息。这些信息可以包含当前常量和变量的值。
|
||||
与 Cocoa 里的`NSLog`函数类似的是,`print`函数可以输出更复杂的信息。这些信息可以包含当前常量和变量的值。
|
||||
|
||||
Swift 用_字符串插值(string interpolation)_的方式把常量名或者变量名当做占位符加入到长字符串中,Swift 会用当前常量或变量的值替换这些占位符。将常量或变量名放入圆括号中,并在开括号前使用反斜杠将其转义:
|
||||
|
||||
```swift
|
||||
println("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
print("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
// 输出 "The current value of friendlyWelcome is Bonjour!
|
||||
```
|
||||
|
||||
@ -181,7 +181,7 @@ Swift 中的注释与C 语言的注释非常相似。单行注释以双正斜杠
|
||||
与其他大部分编程语言不同,Swift 并不强制要求你在每条语句的结尾处使用分号(`;`),当然,你也可以按照你自己的习惯添加分号。有一种情况下必须要用分号,即你打算在同一行内写多条独立的语句:
|
||||
|
||||
```swift
|
||||
let cat = "🐱"; println(cat)
|
||||
let cat = "🐱"; print(cat)
|
||||
// 输出 "🐱"
|
||||
```
|
||||
|
||||
@ -409,9 +409,9 @@ let turnipsAreDelicious = false
|
||||
|
||||
```swift
|
||||
if turnipsAreDelicious {
|
||||
println("Mmm, tasty turnips!")
|
||||
print("Mmm, tasty turnips!")
|
||||
} else {
|
||||
println("Eww, turnips are horrible.")
|
||||
print("Eww, turnips are horrible.")
|
||||
}
|
||||
// 输出 "Eww, turnips are horrible."
|
||||
```
|
||||
@ -460,9 +460,9 @@ let http404Error = (404, "Not Found")
|
||||
|
||||
```swift
|
||||
let (statusCode, statusMessage) = http404Error
|
||||
println("The status code is \(statusCode)")
|
||||
print("The status code is \(statusCode)")
|
||||
// 输出 "The status code is 404"
|
||||
println("The status message is \(statusMessage)")
|
||||
print("The status message is \(statusMessage)")
|
||||
// 输出 "The status message is Not Found"
|
||||
```
|
||||
|
||||
@ -470,16 +470,16 @@ println("The status message is \(statusMessage)")
|
||||
|
||||
```swift
|
||||
let (justTheStatusCode, _) = http404Error
|
||||
println("The status code is \(justTheStatusCode)")
|
||||
print("The status code is \(justTheStatusCode)")
|
||||
// 输出 "The status code is 404"
|
||||
```
|
||||
|
||||
此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:
|
||||
|
||||
```swift
|
||||
println("The status code is \(http404Error.0)")
|
||||
print("The status code is \(http404Error.0)")
|
||||
// 输出 "The status code is 404"
|
||||
println("The status message is \(http404Error.1)")
|
||||
print("The status message is \(http404Error.1)")
|
||||
// 输出 "The status message is Not Found"
|
||||
```
|
||||
|
||||
@ -492,9 +492,9 @@ let http200Status = (statusCode: 200, description: "OK")
|
||||
给元组中的元素命名后,你可以通过名字来获取这些元素的值:
|
||||
|
||||
```swift
|
||||
println("The status code is \(http200Status.statusCode)")
|
||||
print("The status code is \(http200Status.statusCode)")
|
||||
// 输出 "The status code is 200"
|
||||
println("The status message is \(http200Status.description)")
|
||||
print("The status message is \(http200Status.description)")
|
||||
// 输出 "The status message is OK"
|
||||
```
|
||||
|
||||
@ -537,9 +537,9 @@ let convertedNumber = possibleNumber.toInt()
|
||||
|
||||
```swift
|
||||
if convertedNumber != nil {
|
||||
println("\(possibleNumber) has an integer value of \(convertedNumber!)")
|
||||
print("\(possibleNumber) has an integer value of \(convertedNumber!)")
|
||||
} else {
|
||||
println("\(possibleNumber) could not be converted to an integer")
|
||||
print("\(possibleNumber) could not be converted to an integer")
|
||||
}
|
||||
// 输出 "123 has an integer value of 123"
|
||||
```
|
||||
@ -566,9 +566,9 @@ if let constantName = someOptional {
|
||||
|
||||
```swift
|
||||
if let actualNumber = possibleNumber.toInt() {
|
||||
println("\(possibleNumber) has an integer value of \(actualNumber)")
|
||||
print("\(possibleNumber) has an integer value of \(actualNumber)")
|
||||
} else {
|
||||
println("\(possibleNumber) could not be converted to an integer")
|
||||
print("\(possibleNumber) could not be converted to an integer")
|
||||
}
|
||||
// 输出 "123 has an integer value of 123"
|
||||
```
|
||||
@ -619,13 +619,13 @@ Swift 的`nil`和 Objective-C 中的`nil`并不一样。在 Objective-C 中,`n
|
||||
|
||||
```swift
|
||||
let possibleString: String? = "An optional string."
|
||||
println(possibleString!) // 需要惊叹号来获取值
|
||||
print(possibleString!) // 需要惊叹号来获取值
|
||||
// 输出 "An optional string."
|
||||
```
|
||||
|
||||
```swift
|
||||
let assumedString: String! = "An implicitly unwrapped optional string."
|
||||
println(assumedString) // 不需要感叹号
|
||||
print(assumedString) // 不需要感叹号
|
||||
// 输出 "An implicitly unwrapped optional string."
|
||||
```
|
||||
|
||||
@ -638,7 +638,7 @@ println(assumedString) // 不需要感叹号
|
||||
|
||||
```swift
|
||||
if assumedString {
|
||||
println(assumedString)
|
||||
print(assumedString)
|
||||
}
|
||||
// 输出 "An implicitly unwrapped optional string."
|
||||
```
|
||||
@ -647,7 +647,7 @@ if assumedString {
|
||||
|
||||
```swift
|
||||
if let definiteString = assumedString {
|
||||
println(definiteString)
|
||||
print(definiteString)
|
||||
}
|
||||
// 输出 "An implicitly unwrapped optional string."
|
||||
```
|
||||
|
||||
@ -233,9 +233,9 @@ Swift 也提供恒等`===`和不恒等`!==`这两个比较符来判断两个对
|
||||
|
||||
let name = "world"
|
||||
if name == "world" {
|
||||
println("hello, world")
|
||||
print("hello, world")
|
||||
} else {
|
||||
println("I'm sorry \(name), but I don't recognize you")
|
||||
print("I'm sorry \(name), but I don't recognize you")
|
||||
}
|
||||
// 输出 "hello, world", 因为 `name` 就是等于 "world"
|
||||
|
||||
@ -326,7 +326,7 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
闭区间运算符在迭代一个区间的所有值时是非常有用的,如在`for-in`循环中:
|
||||
|
||||
for index in 1...5 {
|
||||
println("\(index) * 5 = \(index * 5)")
|
||||
print("\(index) * 5 = \(index * 5)")
|
||||
}
|
||||
// 1 * 5 = 5
|
||||
// 2 * 5 = 10
|
||||
@ -348,7 +348,7 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
let names = ["Anna", "Alex", "Brian", "Jack"]
|
||||
let count = names.count
|
||||
for i in 0..<count {
|
||||
println("第 \(i + 1) 个人叫 \(names[i])")
|
||||
print("第 \(i + 1) 个人叫 \(names[i])")
|
||||
}
|
||||
// 第 1 个人叫 Anna
|
||||
// 第 2 个人叫 Alex
|
||||
@ -375,7 +375,7 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
|
||||
let allowedEntry = false
|
||||
if !allowedEntry {
|
||||
println("ACCESS DENIED")
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "ACCESS DENIED"
|
||||
|
||||
@ -395,9 +395,9 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
let enteredDoorCode = true
|
||||
let passedRetinaScan = false
|
||||
if enteredDoorCode && passedRetinaScan {
|
||||
println("Welcome!")
|
||||
print("Welcome!")
|
||||
} else {
|
||||
println("ACCESS DENIED")
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "ACCESS DENIED"
|
||||
|
||||
@ -413,9 +413,9 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
let hasDoorKey = false
|
||||
let knowsOverridePassword = true
|
||||
if hasDoorKey || knowsOverridePassword {
|
||||
println("Welcome!")
|
||||
print("Welcome!")
|
||||
} else {
|
||||
println("ACCESS DENIED")
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
|
||||
@ -424,9 +424,9 @@ Swift 提供了两个方便表达一个区间的值的运算符。
|
||||
我们可以组合多个逻辑运算来表达一个复合逻辑:
|
||||
|
||||
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
|
||||
println("Welcome!")
|
||||
print("Welcome!")
|
||||
} else {
|
||||
println("ACCESS DENIED")
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
|
||||
@ -445,9 +445,9 @@ Swift 逻辑操作符`&&`和`||`是左结合的,这意味着拥有多元逻辑
|
||||
|
||||
|
||||
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
|
||||
println("Welcome!")
|
||||
print("Welcome!")
|
||||
} else {
|
||||
println("ACCESS DENIED")
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
|
||||
|
||||
@ -39,13 +39,13 @@ func sayHello(personName: String) -> String {
|
||||
该定义描述了函数做什么,它期望接收什么和执行结束时它返回的结果是什么。这样的定义使得函数可以在别的地方以一种清晰的方式被调用:
|
||||
|
||||
```swift
|
||||
println(sayHello("Anna"))
|
||||
print(sayHello("Anna"))
|
||||
// prints "Hello, Anna!"
|
||||
println(sayHello("Brian"))
|
||||
print(sayHello("Brian"))
|
||||
// prints "Hello, Brian!"
|
||||
```
|
||||
|
||||
调用 `sayHello(_:)` 函数时,在圆括号中传给它一个 `String` 类型的实参。因为这个函数返回一个 `String` 类型的值,`sayHello` 可以被包含在 `println` 的调用中,用来输出这个函数的返回值,正如上面所示。
|
||||
调用 `sayHello(_:)` 函数时,在圆括号中传给它一个 `String` 类型的实参。因为这个函数返回一个 `String` 类型的值,`sayHello` 可以被包含在 `print` 的调用中,用来输出这个函数的返回值,正如上面所示。
|
||||
|
||||
在 `sayHello(_:)` 的函数体中,先定义了一个新的名为 `greeting` 的 `String` 常量,同时赋值了给 `personName` 的一个简单问候消息。然后用 `return` 关键字把这个问候返回出去。一旦 `return greeting` 被调用,该函数结束它的执行并返回 `greeting` 的当前值。
|
||||
|
||||
@ -57,7 +57,7 @@ println(sayHello("Brian"))
|
||||
func sayHelloAgain(personName: String) -> String {
|
||||
return "Hello again, " + personName + "!"
|
||||
}
|
||||
println(sayHelloAgain("Anna"))
|
||||
print(sayHelloAgain("Anna"))
|
||||
// prints "Hello again, Anna!"
|
||||
```
|
||||
|
||||
@ -76,7 +76,7 @@ println(sayHelloAgain("Anna"))
|
||||
func halfOpenRangeLength(start: Int, end: Int) -> Int {
|
||||
return end - start
|
||||
}
|
||||
println(halfOpenRangeLength(1, 10))
|
||||
print(halfOpenRangeLength(1, 10))
|
||||
// prints "9"
|
||||
```
|
||||
|
||||
@ -88,7 +88,7 @@ println(halfOpenRangeLength(1, 10))
|
||||
func sayHelloWorld() -> String {
|
||||
return "hello, world"
|
||||
}
|
||||
println(sayHelloWorld())
|
||||
print(sayHelloWorld())
|
||||
// prints "hello, world"
|
||||
```
|
||||
|
||||
@ -122,7 +122,7 @@ print(sayHello("Tim", alreadyGreeted: true))
|
||||
|
||||
```swift
|
||||
func sayGoodbye(personName: String) {
|
||||
println("Goodbye, \(personName)!")
|
||||
print("Goodbye, \(personName)!")
|
||||
}
|
||||
sayGoodbye("Dave")
|
||||
// prints "Goodbye, Dave!"
|
||||
|
||||
@ -98,7 +98,7 @@ let someVideoMode = VideoMode()
|
||||
通过使用*点语法*(*dot syntax*),你可以访问实例中所含有的属性。其语法规则是,实例名后面紧跟属性名,两者通过点号(.)连接:
|
||||
|
||||
```swift
|
||||
println("The width of someResolution is \(someResolution.width)")
|
||||
print("The width of someResolution is \(someResolution.width)")
|
||||
// 输出 "The width of someResolution is 0"
|
||||
```
|
||||
|
||||
@ -107,7 +107,7 @@ println("The width of someResolution is \(someResolution.width)")
|
||||
你也可以访问子属性,如`VideoMode`中`Resolution`属性的`width`属性:
|
||||
|
||||
```swift
|
||||
println("The width of someVideoMode is \(someVideoMode.resolution.width)")
|
||||
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
|
||||
// 输出 "The width of someVideoMode is 0"
|
||||
```
|
||||
|
||||
@ -115,7 +115,7 @@ println("The width of someVideoMode is \(someVideoMode.resolution.width)")
|
||||
|
||||
```swift
|
||||
someVideoMode.resolution.width = 1280
|
||||
println("The width of someVideoMode is now \(someVideoMode.resolution.width)")
|
||||
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
|
||||
// 输出 "The width of someVideoMode is now 1280"
|
||||
```
|
||||
|
||||
@ -161,14 +161,14 @@ cinema.width = 2048
|
||||
这里,将会显示`cinema`的`width`属性确已改为了`2048`:
|
||||
|
||||
```swift
|
||||
println("cinema is now \(cinema.width) pixels wide")
|
||||
print("cinema is now \(cinema.width) pixels wide")
|
||||
// 输出 "cinema is now 2048 pixels wide"
|
||||
```
|
||||
|
||||
然而,初始的`hd`实例中`width`属性还是`1920`:
|
||||
|
||||
```swift
|
||||
println("hd is still \(hd.width ) pixels wide")
|
||||
print("hd is still \(hd.width ) pixels wide")
|
||||
// 输出 "hd is still 1920 pixels wide"
|
||||
```
|
||||
|
||||
@ -184,7 +184,7 @@ var currentDirection = CompassPoint.West
|
||||
let rememberedDirection = currentDirection
|
||||
currentDirection = .East
|
||||
if rememberedDirection == .West {
|
||||
println("The remembered direction is still .West")
|
||||
print("The remembered direction is still .West")
|
||||
}
|
||||
// 输出 "The remembered direction is still .West"
|
||||
```
|
||||
@ -220,7 +220,7 @@ alsoTenEighty.frameRate = 30.0
|
||||
下面,通过查看`tenEighty`的`frameRate`属性,我们会发现它正确的显示了基本`VideoMode`实例的新帧率,其值为`30.0`:
|
||||
|
||||
```swift
|
||||
println("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
|
||||
print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
|
||||
// 输出 "The frameRate property of theEighty is now 30.0"
|
||||
```
|
||||
|
||||
@ -239,7 +239,7 @@ println("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
|
||||
|
||||
```swift
|
||||
if tenEighty === alsoTenEighty {
|
||||
println("tenEighty and alsoTenEighty refer to the same Resolution instance.")
|
||||
print("tenEighty and alsoTenEighty refer to the same Resolution instance.")
|
||||
}
|
||||
//输出 "tenEighty and alsoTenEighty refer to the same Resolution instance."
|
||||
```
|
||||
@ -288,4 +288,3 @@ Objective-C中`字符串(NSString)`,`数组(NSArray)`和`字典(NSDict
|
||||
> 注意:
|
||||
以上是对于字符串、数组、字典和其它值的`拷贝`的描述。
|
||||
在你的代码中,拷贝好像是确实是在有拷贝行为的地方产生过。然而,在 Swift 的后台中,只有确有必要,`实际(actual)`拷贝才会被执行。Swift 管理所有的值拷贝以确保性能最优化的性能,所以你也没有必要去避免赋值以保证最优性能。(实际赋值由系统管理优化)
|
||||
|
||||
|
||||
@ -54,7 +54,7 @@ struct TimesTable {
|
||||
}
|
||||
}
|
||||
let threeTimesTable = TimesTable(multiplier: 3)
|
||||
println("3的6倍是\(threeTimesTable[6])")
|
||||
print("3的6倍是\(threeTimesTable[6])")
|
||||
// 输出 "3的6倍是18"
|
||||
```
|
||||
|
||||
|
||||
@ -49,7 +49,7 @@ let someVehicle = Vehicle()
|
||||
现在已经创建了一个`Vehicle`的新实例,你可以访问它的`description`属性来打印车辆的当前速度。
|
||||
|
||||
```swift
|
||||
println("Vehicle: \(someVehicle.description)")
|
||||
print("Vehicle: \(someVehicle.description)")
|
||||
// Vehicle: traveling at 0.0 miles per hour
|
||||
```
|
||||
|
||||
@ -91,7 +91,7 @@ bicycle.hasBasket = true
|
||||
|
||||
```swift
|
||||
bicycle.currentSpeed = 15.0
|
||||
println("Bicycle: \(bicycle.description)")
|
||||
print("Bicycle: \(bicycle.description)")
|
||||
// Bicycle: traveling at 15.0 miles per hour
|
||||
```
|
||||
|
||||
@ -112,7 +112,7 @@ let tandem = Tandem()
|
||||
tandem.hasBasket = true
|
||||
tandem.currentNumberOfPassengers = 2
|
||||
tandem.currentSpeed = 22.0
|
||||
println("Tandem: \(tandem.description)")
|
||||
print("Tandem: \(tandem.description)")
|
||||
// Tandem: traveling at 22.0 miles per hour
|
||||
```
|
||||
|
||||
@ -144,7 +144,7 @@ println("Tandem: \(tandem.description)")
|
||||
```swift
|
||||
class Train: Vehicle {
|
||||
override func makeNoise() {
|
||||
println("Choo Choo")
|
||||
print("Choo Choo")
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -189,7 +189,7 @@ class Car: Vehicle {
|
||||
let car = Car()
|
||||
car.currentSpeed = 25.0
|
||||
car.gear = 3
|
||||
println("Car: \(car.description)")
|
||||
print("Car: \(car.description)")
|
||||
// Car: traveling at 25.0 miles per hour in gear 3
|
||||
```
|
||||
|
||||
@ -217,7 +217,7 @@ class AutomaticCar: Car {
|
||||
```swift
|
||||
let automatic = AutomaticCar()
|
||||
automatic.currentSpeed = 35.0
|
||||
println("AutomaticCar: \(automatic.description)")
|
||||
print("AutomaticCar: \(automatic.description)")
|
||||
// AutomaticCar: traveling at 35.0 miles per hour in gear 4
|
||||
```
|
||||
|
||||
@ -229,4 +229,3 @@ println("AutomaticCar: \(automatic.description)")
|
||||
如果你重写了`final`方法,属性或下标脚本,在编译时会报错。在类扩展中的方法,属性或下标脚本也可以在扩展的定义里标记为 final。
|
||||
|
||||
你可以通过在关键字`class`前添加`final`特性(`final class`)来将整个类标记为 final 的,这样的类是不可被继承的,任何子类试图继承此类时,在编译时会报错。
|
||||
|
||||
|
||||
@ -53,7 +53,7 @@ struct Fahrenheit {
|
||||
}
|
||||
}
|
||||
var f = Fahrenheit()
|
||||
println("The default temperature is \(f.temperature)° Fahrenheit")
|
||||
print("The default temperature is \(f.temperature)° Fahrenheit")
|
||||
// 输出 "The default temperature is 32.0° Fahrenheit”
|
||||
```
|
||||
|
||||
@ -182,7 +182,7 @@ class SurveyQuestion {
|
||||
self.text = text
|
||||
}
|
||||
func ask() {
|
||||
println(text)
|
||||
print(text)
|
||||
}
|
||||
}
|
||||
let cheeseQuestion = SurveyQuestion(text: "Do you like cheese?")
|
||||
@ -210,7 +210,7 @@ class SurveyQuestion {
|
||||
self.text = text
|
||||
}
|
||||
func ask() {
|
||||
println(text)
|
||||
print(text)
|
||||
}
|
||||
}
|
||||
let beetsQuestion = SurveyQuestion(text: "How about beets?")
|
||||
@ -656,7 +656,7 @@ var breakfastList = [
|
||||
breakfastList[0].name = "Orange juice"
|
||||
breakfastList[0].purchased = true
|
||||
for item in breakfastList {
|
||||
println(item.description)
|
||||
print(item.description)
|
||||
}
|
||||
// 1 x orange juice ✔
|
||||
// 1 x bacon ✘
|
||||
@ -699,7 +699,7 @@ let someCreature = Animal(species: "Giraffe")
|
||||
// someCreature 的类型是 Animal? 而不是 Animal
|
||||
|
||||
if let giraffe = someCreature {
|
||||
println("An animal was initialized with a species of \(giraffe.species)")
|
||||
print("An animal was initialized with a species of \(giraffe.species)")
|
||||
}
|
||||
// 打印 "An animal was initialized with a species of Giraffe"
|
||||
```
|
||||
@ -711,7 +711,7 @@ let anonymousCreature = Animal(species: "")
|
||||
// anonymousCreature 的类型是 Animal?, 而不是 Animal
|
||||
|
||||
if anonymousCreature == nil {
|
||||
println("The anonymous creature could not be initialized")
|
||||
print("The anonymous creature could not be initialized")
|
||||
}
|
||||
// 打印 "The anonymous creature could not be initialized"
|
||||
```
|
||||
@ -748,13 +748,13 @@ enum TemperatureUnit {
|
||||
```swift
|
||||
let fahrenheitUnit = TemperatureUnit(symbol: "F")
|
||||
if fahrenheitUnit != nil {
|
||||
println("This is a defined temperature unit, so initialization succeeded.")
|
||||
print("This is a defined temperature unit, so initialization succeeded.")
|
||||
}
|
||||
// 打印 "This is a defined temperature unit, so initialization succeeded."
|
||||
|
||||
let unknownUnit = TemperatureUnit(symbol: "X")
|
||||
if unknownUnit == nil {
|
||||
println("This is not a defined temperature unit, so initialization failed.")
|
||||
print("This is not a defined temperature unit, so initialization failed.")
|
||||
}
|
||||
// 打印 "This is not a defined temperature unit, so initialization failed."
|
||||
```
|
||||
@ -772,13 +772,13 @@ enum TemperatureUnit: Character {
|
||||
|
||||
let fahrenheitUnit = TemperatureUnit(rawValue: "F")
|
||||
if fahrenheitUnit != nil {
|
||||
println("This is a defined temperature unit, so initialization succeeded.")
|
||||
print("This is a defined temperature unit, so initialization succeeded.")
|
||||
}
|
||||
// prints "This is a defined temperature unit, so initialization succeeded."
|
||||
|
||||
let unknownUnit = TemperatureUnit(rawValue: "X")
|
||||
if unknownUnit == nil {
|
||||
println("This is not a defined temperature unit, so initialization failed.")
|
||||
print("This is not a defined temperature unit, so initialization failed.")
|
||||
}
|
||||
// prints "This is not a defined temperature unit, so initialization failed."
|
||||
```
|
||||
@ -804,7 +804,7 @@ class Product {
|
||||
```swift
|
||||
if let bowTie = Product(name: "bow tie") {
|
||||
// 不需要检查 bowTie.name == nil
|
||||
println("The product's name is \(bowTie.name)")
|
||||
print("The product's name is \(bowTie.name)")
|
||||
}
|
||||
// 打印 "The product's name is bow tie"
|
||||
```
|
||||
@ -841,7 +841,7 @@ class CartItem: Product {
|
||||
|
||||
```swift
|
||||
if let twoSocks = CartItem(name: "sock", quantity: 2) {
|
||||
println("Item: \(twoSocks.name), quantity: \(twoSocks.quantity)")
|
||||
print("Item: \(twoSocks.name), quantity: \(twoSocks.quantity)")
|
||||
}
|
||||
// 打印 "Item: sock, quantity: 2"
|
||||
```
|
||||
@ -849,9 +849,9 @@ if let twoSocks = CartItem(name: "sock", quantity: 2) {
|
||||
|
||||
```swift
|
||||
if let zeroShirts = CartItem(name: "shirt", quantity: 0) {
|
||||
println("Item: \(zeroShirts.name), quantity: \(zeroShirts.quantity)")
|
||||
print("Item: \(zeroShirts.name), quantity: \(zeroShirts.quantity)")
|
||||
} else {
|
||||
println("Unable to initialize zero shirts")
|
||||
print("Unable to initialize zero shirts")
|
||||
}
|
||||
// 打印 "Unable to initialize zero shirts"
|
||||
```
|
||||
@ -860,9 +860,9 @@ if let zeroShirts = CartItem(name: "shirt", quantity: 0) {
|
||||
|
||||
```swift
|
||||
if let oneUnnamed = CartItem(name: "", quantity: 1) {
|
||||
println("Item: \(oneUnnamed.name), quantity: \(oneUnnamed.quantity)")
|
||||
print("Item: \(oneUnnamed.name), quantity: \(oneUnnamed.quantity)")
|
||||
} else {
|
||||
println("Unable to initialize one unnamed product")
|
||||
print("Unable to initialize one unnamed product")
|
||||
}
|
||||
// 打印 "Unable to initialize one unnamed product"
|
||||
```
|
||||
@ -1001,9 +1001,8 @@ struct Checkerboard {
|
||||
|
||||
```swift
|
||||
let board = Checkerboard()
|
||||
println(board.squareIsBlackAtRow(0, column: 1))
|
||||
print(board.squareIsBlackAtRow(0, column: 1))
|
||||
// 输出 "true"
|
||||
println(board.squareIsBlackAtRow(9, column: 9))
|
||||
print(board.squareIsBlackAtRow(9, column: 9))
|
||||
// 输出 "false"
|
||||
```
|
||||
|
||||
|
||||
@ -77,9 +77,9 @@ class Player {
|
||||
|
||||
```swift
|
||||
var playerOne: Player? = Player(coins: 100)
|
||||
println("A new player has joined the game with \(playerOne!.coinsInPurse) coins")
|
||||
print("A new player has joined the game with \(playerOne!.coinsInPurse) coins")
|
||||
// 输出 "A new player has joined the game with 100 coins"
|
||||
println("There are now \(Bank.coinsInBank) coins left in the bank")
|
||||
print("There are now \(Bank.coinsInBank) coins left in the bank")
|
||||
// 输出 "There are now 9900 coins left in the bank"
|
||||
```
|
||||
|
||||
@ -89,9 +89,9 @@ println("There are now \(Bank.coinsInBank) coins left in the bank")
|
||||
|
||||
```swift
|
||||
playerOne!.winCoins(2_000)
|
||||
println("PlayerOne won 2000 coins & now has \(playerOne!.coinsInPurse) coins")
|
||||
print("PlayerOne won 2000 coins & now has \(playerOne!.coinsInPurse) coins")
|
||||
// 输出 "PlayerOne won 2000 coins & now has 2100 coins"
|
||||
println("The bank now only has \(Bank.coinsInBank) coins left")
|
||||
print("The bank now only has \(Bank.coinsInBank) coins left")
|
||||
// 输出 "The bank now only has 7900 coins left"
|
||||
```
|
||||
|
||||
@ -99,9 +99,9 @@ println("The bank now only has \(Bank.coinsInBank) coins left")
|
||||
|
||||
```swift
|
||||
playerOne = nil
|
||||
println("PlayerOne has left the game")
|
||||
print("PlayerOne has left the game")
|
||||
// 输出 "PlayerOne has left the game"
|
||||
println("The bank now has \(Bank.coinsInBank) coins")
|
||||
print("The bank now has \(Bank.coinsInBank) coins")
|
||||
// 输出 "The bank now has 10000 coins"
|
||||
```
|
||||
|
||||
|
||||
@ -557,4 +557,3 @@ print(paragraph!.asHTML())
|
||||
paragraph = nil
|
||||
// prints "p is being deinitialized"
|
||||
```
|
||||
|
||||
|
||||
@ -62,9 +62,9 @@ let roomCount = john.residence!.numberOfRooms
|
||||
|
||||
```swift
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
print("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
print("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms.
|
||||
```
|
||||
@ -85,9 +85,9 @@ john.residence = Residence()
|
||||
|
||||
```swift
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
print("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
print("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "John's residence has 1 room(s)"。
|
||||
```
|
||||
@ -119,7 +119,7 @@ class Residence {
|
||||
return rooms[i]
|
||||
}
|
||||
func printNumberOfRooms() {
|
||||
println("The number of rooms is \(numberOfRooms)")
|
||||
print("The number of rooms is \(numberOfRooms)")
|
||||
}
|
||||
var address: Address?
|
||||
}
|
||||
@ -173,9 +173,9 @@ class Address {
|
||||
```swift
|
||||
let john = Person()
|
||||
if let roomCount = john.residence?.numberOfRooms {
|
||||
println("John's residence has \(roomCount) room(s).")
|
||||
print("John's residence has \(roomCount) room(s).")
|
||||
} else {
|
||||
println("Unable to retrieve the number of rooms.")
|
||||
print("Unable to retrieve the number of rooms.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the number of rooms。
|
||||
```
|
||||
@ -191,7 +191,7 @@ if let roomCount = john.residence?.numberOfRooms {
|
||||
|
||||
```swift
|
||||
func printNumberOfRooms(){
|
||||
println(“The number of rooms is \(numberOfRooms)”)
|
||||
print(“The number of rooms is \(numberOfRooms)”)
|
||||
}
|
||||
```
|
||||
|
||||
@ -201,9 +201,9 @@ func printNumberOfRooms(){
|
||||
|
||||
```swift
|
||||
if john.residence?.printNumberOfRooms?() {
|
||||
println("It was possible to print the number of rooms.")
|
||||
print("It was possible to print the number of rooms.")
|
||||
} else {
|
||||
println("It was not possible to print the number of rooms.")
|
||||
print("It was not possible to print the number of rooms.")
|
||||
}
|
||||
// 打印 "It was not possible to print the number of rooms."。
|
||||
```
|
||||
@ -220,9 +220,9 @@ if john.residence?.printNumberOfRooms?() {
|
||||
|
||||
```swift
|
||||
if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
print("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
print("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the first room name."。
|
||||
```
|
||||
@ -238,9 +238,9 @@ johnsHouse.rooms += Room(name: "Kitchen")
|
||||
john.residence = johnsHouse
|
||||
|
||||
if let firstRoomName = john.residence?[0].name {
|
||||
println("The first room name is \(firstRoomName).")
|
||||
print("The first room name is \(firstRoomName).")
|
||||
} else {
|
||||
println("Unable to retrieve the first room name.")
|
||||
print("Unable to retrieve the first room name.")
|
||||
}
|
||||
// 打印 "The first room name is Living Room."。
|
||||
```
|
||||
@ -263,9 +263,9 @@ if let firstRoomName = john.residence?[0].name {
|
||||
|
||||
```swift
|
||||
if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
print("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
print("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "Unable to retrieve the address.”。
|
||||
```
|
||||
@ -285,9 +285,9 @@ john.residence!.address = johnsAddress
|
||||
|
||||
```swift
|
||||
if let johnsStreet = john.residence?.address?.street {
|
||||
println("John's street name is \(johnsStreet).")
|
||||
print("John's street name is \(johnsStreet).")
|
||||
} else {
|
||||
println("Unable to retrieve the address.")
|
||||
print("Unable to retrieve the address.")
|
||||
}
|
||||
// 打印 "John's street name is Laurel Street."。
|
||||
```
|
||||
@ -303,7 +303,7 @@ if let johnsStreet = john.residence?.address?.street {
|
||||
|
||||
```swift
|
||||
if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
|
||||
println("John's building identifier is \(buildingIdentifier).")
|
||||
print("John's building identifier is \(buildingIdentifier).")
|
||||
}
|
||||
// 打印 "John's building identifier is The Larches."。
|
||||
```
|
||||
@ -312,7 +312,7 @@ if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
|
||||
|
||||
```swift
|
||||
if let upper = john.residence?.address?.buildingIdentifier()?.uppercaseString {
|
||||
println("John's uppercase building identifier is \(upper).")
|
||||
print("John's uppercase building identifier is \(upper).")
|
||||
}
|
||||
// 打印 "John's uppercase building identifier is THE LARCHES."。
|
||||
```
|
||||
|
||||
@ -93,4 +93,3 @@ let heartsSymbol = BlackjackCard.Suit.Hearts.rawValue
|
||||
```
|
||||
|
||||
对于上面这个例子,这样可以使`Suit`, `Rank`, 和 `Values`的名字尽可能的短,因为它们的名字会自然的由定义它们的上下文来限定。
|
||||
|
||||
|
||||
@ -67,10 +67,10 @@ extension Double {
|
||||
var ft: Double { return self / 3.28084 }
|
||||
}
|
||||
let oneInch = 25.4.mm
|
||||
println("One inch is \(oneInch) meters")
|
||||
print("One inch is \(oneInch) meters")
|
||||
// 打印输出:"One inch is 0.0254 meters"
|
||||
let threeFeet = 3.ft
|
||||
println("Three feet is \(threeFeet) meters")
|
||||
print("Three feet is \(threeFeet) meters")
|
||||
// 打印输出:"Three feet is 0.914399970739201 meters"
|
||||
```
|
||||
|
||||
@ -84,7 +84,7 @@ println("Three feet is \(threeFeet) meters")
|
||||
|
||||
```swift
|
||||
let aMarathon = 42.km + 195.m
|
||||
println("A marathon is \(aMarathon) meters long")
|
||||
print("A marathon is \(aMarathon) meters long")
|
||||
// 打印输出:"A marathon is 42195.0 meters long"
|
||||
```
|
||||
|
||||
@ -169,7 +169,7 @@ extension Int {
|
||||
|
||||
```swift
|
||||
3.repetitions({
|
||||
println("Hello!")
|
||||
print("Hello!")
|
||||
})
|
||||
// Hello!
|
||||
// Hello!
|
||||
@ -180,7 +180,7 @@ extension Int {
|
||||
|
||||
```swift
|
||||
3.repetitions{
|
||||
println("Goodbye!")
|
||||
print("Goodbye!")
|
||||
}
|
||||
// Goodbye!
|
||||
// Goodbye!
|
||||
@ -296,5 +296,3 @@ printIntegerKinds([3, 19, -27, 0, -6, 0, 7])
|
||||
|
||||
>注意:
|
||||
由于已知`number.kind `是`Int.Kind`型,所以`Int.Kind`中的所有成员值都可以使用`switch`语句里的形式简写,比如使用 `. Negative`代替`Int.Kind.Negative`。
|
||||
|
||||
|
||||
|
||||
@ -266,7 +266,7 @@ var stringToEdit = TrackedString()
|
||||
stringToEdit.value = "This string will be tracked."
|
||||
stringToEdit.value += " This edit will increment numberOfEdits."
|
||||
stringToEdit.value += " So will this one."
|
||||
println("The number of edits is \(stringToEdit.numberOfEdits)")
|
||||
print("The number of edits is \(stringToEdit.numberOfEdits)")
|
||||
// prints "The number of edits is 3"
|
||||
```
|
||||
|
||||
@ -345,6 +345,3 @@ Swift为结构体、类都提供了一个默认的无参初始化方法,用于
|
||||
任何你定义的类型别名都会被当作不同的类型,以便于进行访问控制。一个类型别名的访问级别不可高于原类型的访问级别。比如说,一个`private`级别的类型别名可以设定给一个`public`、`internal`、`private`的类型,但是一个`public`级别的类型别名只能设定给一个`public`级别的类型,不能设定给`internal`或`private` 级别的类型。
|
||||
|
||||
> 注意:这条规则也适用于为满足协议一致性而给相关类型命名别名的情况。
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user