@ -682,10 +682,10 @@ protocolValue.simpleDescription
|
|||||||
在尖括号里写一个名字来创建一个泛型函数或者类型。
|
在尖括号里写一个名字来创建一个泛型函数或者类型。
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {
|
func repeat<ItemType>(item: ItemType, times: Int) -> [ItemType] {
|
||||||
var result = ItemType[]()
|
var result = [ItemType]()
|
||||||
for i in 0..times {
|
for i in 0..<times {
|
||||||
result += item
|
result.append(item)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -707,7 +707,7 @@ possibleInteger = .Some(100)
|
|||||||
在类型名后面使用`where`来指定对类型的需求,比如,限定类型实现某一个协议,限定两个类型是相同的,或者限定某个类必须有一个特定的父类
|
在类型名后面使用`where`来指定对类型的需求,比如,限定类型实现某一个协议,限定两个类型是相同的,或者限定某个类必须有一个特定的父类
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
|
func anyCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> Bool {
|
||||||
for lhsItem in lhs {
|
for lhsItem in lhs {
|
||||||
for rhsItem in rhs {
|
for rhsItem in rhs {
|
||||||
if lhsItem == rhsItem {
|
if lhsItem == rhsItem {
|
||||||
|
|||||||
Reference in New Issue
Block a user