可以直接在多行字符串中包含"
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
可以直接在多行字符串中包含"""
let threeDoubleQuotes = """
Escaping the first quote \"""
Escaping all three quotes \"\"\"
"""
以下两个字符串是等价的:
let singleLineString = "These are the same."
let multilineString = """
These are the same.
"""
在字符串前后加空行,可以这样写:
"""
This string starts with a line feed.
It also ends with a line feed.
"""
如果一个多行字符串定义在一个方法中,实际字符串的值是不包含每一行前面的空格:
func generateQuotation() -> String {
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
return quotation
}
print(quotation == generateQuotation())
// Prints "true"
如果把字符串改为下面的这个样子,第二行字符串前面的空格是不能忽略的:
func generateQuotation() -> String {
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
return quotation
}
print(quotation == generateQuotation())
// Prints "false"
下标可以是泛型的,并且可以包含where
语句:
extension Container {
subscript<Indices: Sequence>(indices: Indices) -> [Item]
where Indices.Iterator.Element == Int {
var result = [Item]()
for index in indices {
result.append(self[index])
}
return result
}
}
上述代码的意思是:接受一个下标序列,并返回对应的元素组成的数组:
Indices
必须遵循Sequence
协议indices
必须是Indices
类型where
语句要求Indices
的元素必须是Int
类型
例如下面这个例子:
class Location {
var latitude: Double
var longitude: Double
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
class City: Location, Named {
var name: String
init(name: String, latitude: Double, longitude: Double) {
self.name = name
super.init(latitude: latitude, longitude: longitude)
}
}
func beginConcert(in location: Location & Named) {
print("Hello, \(location.name)!")
}
let seattle = City(name: "Seattle", latitude: 47.6, longitude: -122.3)
beginConcert(in: seattle)
// Prints "Hello, Seattle!"
在beginConcert
方法中,location
参数必须Location
类型,并且遵循Named
协议。
在协议扩展里面,我们不能使用final
。