Open jiangleligejiang opened 5 years ago
typealias
的使用typealias
使代码更规范
//定义一个<k,v>为<String,String>的Dictionary
typealias RowModel = [String : String]
//使用更具意义的名词来代替类型名词 typealias Location = CGPoint typealias Distance = Double func distanceBetweenPoint(location: Location, toLocation: Location) -> Distance { let dx = Distance(location.x - toLocation.x) let dy = Distance(location.y - toLocation.y) return sqrt(dx dx + dy dy) } let origin: Location = Location(x: 0, y: 0) let point: Location = Location(x: 1, y: 1) let distance: Distance = distanceBetweenPoint(origin, toLocation: point)
- 使用`typealias`时,必须要保证泛型中需要确定类型
```swift
class Person<T> {}
typealias WorkId = Person<String>
typealias Worker = Person<WorkId>
通常情况下,函数传入的参数都为不可变的,若需要参数是引用传递,则要使用
inout
关键字func quick_sort_arr( A: inout Array<Int>, start:Int, end:Int) { if start < end { let partition = quick_sort_partition(A: &A, start: start, end: end) quick_sort_arr(A: &A, start: start, end: partition-1) quick_sort_arr(A: &A, start: partition+1, end: end) } }
由于swift中
protocol
不能使用<T>
这种形式来定义泛型,因此,提供了associatedtype
来定义泛型。public protocol ObservableConvertibleType { /// Type of elements in sequence. associatedtype Element
@available(*, deprecated, message: "Use `Element` instead.")
typealias E = Element
/// Converts `self` to `Observable` sequence.
///
/// - returns: Observable sequence that represents `self`.
func asObservable() -> Observable<Element>
}
使用
struct
更好构建常量的调用}