jiangleligejiang / JNote

记录相关日常笔记
0 stars 0 forks source link

Swift相关语言特性 #7

Open jiangleligejiang opened 5 years ago

jiangleligejiang commented 5 years ago

}

调用方式:
```swift
view.backgroundColor = Specs.color.gray
jiangleligejiang commented 5 years ago

typealias的使用

//使用更具意义的名词来代替类型名词 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>
jiangleligejiang commented 5 years ago

函数参数不变性

通常情况下,函数传入的参数都为不可变的,若需要参数是引用传递,则要使用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)
}
}
jiangleligejiang commented 5 years ago

associatedtype的使用

由于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>

}

jiangleligejiang commented 5 years ago

Swift中的@escaping

jiangleligejiang commented 5 years ago

defer的使用

jiangleligejiang commented 5 years ago

fileprivate和open的使用

jiangleligejiang commented 5 years ago

@inline注解的使用

jiangleligejiang commented 5 years ago

protocol 继承 class