一套 Swift 扩展,用于标准类型和类。
Wiki 中的示例
名称 | 签名 |
---|---|
first |
first () -> Element? |
last |
last () -> Element? |
get |
get (index: Int) -> Element? |
remove |
remove <U: Equatable> (element: U) |
at |
at (indexes: Int...) -> Array |
take |
take (n: Int) -> Array |
takeWhile |
takeWhile (condition: (Element) -> Bool) -> Array |
takeFirst |
takeFirst (condition: (Element) -> Bool) -> Element |
tail |
tail (n: Int) -> Array |
skip |
skip (n: Int) -> Array |
skipWhile |
skipWhile (condition: (Element) -> Bool) -> Array |
contains |
contains <T: Equatable> (item: T...) -> Bool |
difference |
difference <T: Equatable> (values: [T]...) -> [T] |
intersection |
intersection <U: Equatable> (values: [U]...) -> Array |
union |
union <U: Equatable> (values: [U]...) -> Array |
unique |
unique <T: Equatable> () -> [T] |
indexOf |
indexOf <T: Equatable> (item: T) -> Int? |
indexOf |
indexOf (condition: Element -> Bool) -> Int? |
lastIndexOf |
lastIndexOf <T: Equatable> (item: T) -> Int? |
zip |
zip (arrays: Array<Any>...) -> [[Any?]] |
partition |
partition (var n: Int, var step: Int? = nil) -> [Array] partition (var n: Int, var step: Int? = nil, pad: Element[]?) -> [Array]
|
partitionAll |
partitionAll (var n: Int, var step: Int? = nil) -> [Array] |
partitionBy |
partitionBy <T: Equatable> (cond: (Element) -> T) -> [Array] |
shuffle |
shuffle () |
shuffled |
shuffled () -> Array |
sample (random) |
sample (size n: Int = 1) -> [T] |
max |
max <T: Comparable> () -> T |
min |
min <T: Comparable> () -> T |
每个 |
每个 (调用:(Element) -> ()) 每个 (调用:(Int, Element) -> ())
|
eachRight |
eachRight (调用:(Element) -> ()) eachRight (调用:(Int, Element) -> ())
|
任何 |
任何 (调用:(Element) -> Bool) -> Bool |
所有 |
所有 (调用:(Element) -> Bool) -> Bool |
reject |
reject (排除:(Element -> Bool)) -> Array |
pop |
pop() -> Element |
push |
push(newElement: Element) |
shift |
shift() -> Element |
unshift |
unshift(newElement: Element) |
insert |
insert (newArray: Array, atIndex: Int) |
groupBy |
groupBy <U> (groupingFunction group: (Element) -> (U)) -> [U: Array] |
countBy |
countBy <U> (groupingFunction group: (Element) -> (U)) -> [U: Int] |
countWhere |
countWhere (测试:(Element) -> Bool) -> Int |
reduce |
reduce (合并:(Element, Element) -> Element) -> Element? |
reduceRight |
reduceRight <U>(初始值: U, 合并:(U, Element) -> U) -> U |
mapFilter |
mapFilter <V> (映射函数 map: (Element) -> (V)?) -> [V] |
implode |
implode <C: ExtensibleCollection> (分隔符: C) -> C? |
flatten |
flatten <OutType> () -> [OutType] |
flattenAny |
flattenAny () -> [AnyObject] |
sortBy |
sortBy (判断排序:(T, T) -> Bool) -> [T] |
toDictionary |
toDictionary <U> (键选择器:(Element) -> U) -> [U: Element] |
名称 | 签名 |
---|---|
range |
range <U: ForwardIndex> (range: Range<U>) -> Array<U> |
名称 | 签名 | 函数 |
---|---|---|
- |
- <T: Equatable> (第一个: Array<T>, 第二个: Array<T>) -> Array<T> |
差集 |
- |
- <T: Equatable> (第一个: Array<T>, 第二个: T) -> Array<T> |
元素移除 |
& |
& <T: Equatable> (第一个: Array<T>, 第二个: Array<T>) -> Array<T> |
交集 |
| |
| (第一个: Array, 第二个: Array) -> Array |
并集 |
* Int |
* <ItemType> (数组: ItemType[], n: Int) -> [ItemType] |
返回由 self 的 int 次复制组成的新数组 |
* String |
* (数组: String[], 分隔符: String) -> String |
等同于 array.implode(String) |
[x..y] [x...y]
|
下标(range: Range<Int>) -> Array |
返回从索引 x 到索引 y 的子数组 |
[x, y, ...] |
下标(first: Int, second: Int, rest: Int...) -> Array |
返回索引 x、y 的项 |
Wiki 中的示例
名称 | 签名 |
---|---|
times |
times <T> (调用:(Int) -> T) times <T> (调用:(()) -> T) times (调用:(()) -> ())
|
isEven |
isEven () -> Bool |
isOdd |
isOdd () -> Bool |
upTo |
upTo (限制: Int, 调用: (Int) -> ()) |
downTo |
downTo (限制: Int, 调用: (Int) -> ()) |
clamp |
clamp (范围: Range<Int>) -> Int clamp (最小值: Int, 最大值: Int) -> Int
|
isIn |
isIn (范围: Range<Int>, 严格: Bool = false) -> Bool |
digits |
digits () -> Array<Int> |
abs |
abs () -> Int |
gcd |
gcd (n: Int) -> Int |
lcm |
lcm (n: Int) -> Int |
名称 | 签名 |
---|---|
random |
random(min: Int = 0, max: Int) -> Int |
在维基页面上的示例
名称 | 签名 |
---|---|
abs |
abs () -> Float |
sqrt |
sqrt () -> Float |
round |
round () -> Float |
ceil |
ceil () -> Float |
floor |
floor () -> Float |
名称 | 签名 |
---|---|
random |
random(min: Float = 0, max: Float) -> Float |
在维基页面上的示例
名称 |
---|
length |
capitalized |
名称 | 签名 |
---|---|
explode |
explode (separator: Character) -> [String] |
at |
at (indexes: Int...) -> [String] |
matches |
matches (pattern: String, ignoreCase: Bool = false) -> [NSTextCheckingResult]? |
insert |
insert (index: Int, _ string: String) -> String |
ltrimmed |
ltrimmed () -> String |
rtrimmed |
rtrimmed () -> String |
trimmed |
trimmed () -> String |
名称 | 签名 |
---|---|
random |
func random (var length len: Int = 0, charset: String = "...") -> String |
名称 | 签名 |
---|---|
[x] |
subscript(index: Int) -> String? |
[x..y] [x...y]
|
subscript(range: Range |
[x, y, z] |
subscript (indexes: Int...) -> [String] |
S * n |
* (first: String, second: Int) -> String |
=~ |
=~ (string: String, pattern: String) -> Bool =~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> Bool =~ (strings: [String], pattern: String) -> Bool =~ (strings: [String], options: (pattern: String, ignoreCase: Bool)) -> Bool
|
|~ |
|~ (string: String, pattern: String) -> Bool |~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> Bool
|
在维基页面上的示例
名称 | 签名 |
---|---|
times |
times (call: (T) -> ()) times (调用:(()) -> ())
|
每个 |
each (call: (T) -> ()) |
名称 | 签名 |
---|---|
random |
random (from: Int, to: Int) -> Range |
名称 | 签名 | 函数 |
---|---|---|
= |
== <U: ForwardIndex> (first: Range, second: Range) -> Bool |
比较两个范围 |
在维基页面上的示例
名称 | 签名 |
---|---|
difference |
difference <V: Equatable> (dictionaries: [Key: V]...) -> [Key: V] |
union |
union (dictionaries: [Key: Value]...) -> [Key: Value] |
intersection |
intersection <K, V where K: Equatable, V: Equatable> (dictionaries: [K: V]...) -> [K: V] |
has |
has (key: Key) -> Bool |
map |
map <K, V> (mapFunction map: (Key, Value) -> (K, V)) -> [K: V] |
mapFilter |
mapFilter <K, V> (mapFunction map: (Key, Value) -> (K, V)?) -> [K: V] |
mapValues |
mapValues <V> (mapFunction map: (Key, Value) -> (V)) -> [Key: V] |
mapFilterValues |
mapFilterValues <V> (mapFunction map: (Key, Value) -> V?) -> [Key: V] |
每个 |
each(eachFunction each: (Key, Value) -> ()) |
filter |
filter(testFunction test: (Key, Value) -> Bool) -> [Key: Value] |
merge |
merge (dictionaries: [Key: Value]...) -> [Key: Value] |
shift |
shift () -> (Key, Value) |
groupBy |
groupBy <T> (groupingFunction group: (Key, Value) -> (T)) -> [T: Array<Value>] |
countBy |
countBy <T> (groupingFunction group: (Key, Value) -> (T)) -> [T: Int] |
countWhere |
countWhere (test: (Key, Value) -> (Bool)) -> Int |
任何 |
any (test: (Key, Value) -> (Bool)) -> Bool |
所有 |
all (test: (Key, Value) -> (Bool)) -> Bool |
reduce |
reduce <U> (initial: U, combine: (U, Element) -> U) -> U |
pick , at |
pick (keys: [Key]) -> Dictionary pick (keys: Key...) -> Dictionary at (keys: Key...) -> Dictionary
|
名称 | 签名 | 函数 |
---|---|---|
- |
- <K, V: Equatable> (first: Dictionary<K, V>, second: Dictionary<K, V>) -> Dictionary<K, V> |
差集 |
& |
& <K, V: Equatable> (first: Dictionary<K, V>, second: Dictionary<K, V>) -> Dictionary<K, V> |
交集 |
| |
| (first: Dictionary, second: Dictionary) -> Dictionary |
并集 |
在Wiki中的示例
名称 | 签名 |
---|---|
cast |
cast <OutType> () -> [OutType] |
flatten |
flatten <OutType> () -> [OutType] |
flattenAny |
flattenAny () -> [AnyObject] |
以下操作可以在序列上执行,并且是惰性评估的。每次操作只从源序列中获取其数据以返回其结果。
由于.protocol不能扩展,因此以下是对SequenceOf的扩展。可以使用如下方式使用
var source: Sequence = ...
var filteredSequence = SequenceOf(source).filter { ... }
名称 | 签名 |
---|---|
first |
first () -> T? |
任何 |
any (call: (T) -> Bool) -> Bool |
get |
get (index: Int) -> T? |
get |
get (range: Range<Int>) -> SequenceOf<T> |
indexOf |
indexOf <U: Equatable> (item: U) -> Int? |
filter |
filter(include: (T) -> Bool) -> SequenceOf<T> |
reject |
reject (exclude: (T -> Bool)) -> SequenceOf<T> |
skipWhile |
skipWhile(condition:(T) -> Bool) -> SequenceOf<T> |
skip |
skip (n:Int) -> SequenceOf<T> |
contains |
contains<T:Equatable> (item: T) -> Bool |
take |
take (n:Int) -> SequenceOf<T> |
takeWhile |
takeWhile (condition:(T?) -> Bool) -> SequenceOf<T> |
在Wiki中的示例
名称 | 签名 |
---|---|
abs |
abs () -> Double |
sqrt |
sqrt () -> Double |
round |
round () -> Double |
ceil |
ceil () -> Double |
floor |
floor () -> Double |
名称 | 签名 |
---|---|
random |
random(min: Double = 0, max: Double) -> Double |
在Wiki中的示例
名称 | 签名 |
---|---|
after |
after <P, T> (n: Int, function: P -> T) -> (P -> T?) func after <T> (n: Int, function: () -> T) -> (() -> T?)
|
once |
once <P, T> (function: P -> T) -> (P -> T?) once <T> (call: Void -> T) -> (Void -> T?)
|
partial |
partial <P, T> (function: (P...) -> T, _ parameters: P...) -> ((P...) -> T?) |
bind |
bind <P, T> (function: (P...) -> T, _ parameters: P...) -> (() -> T) |
cached |
cached <P, R> (function: (P...) -> R) -> ((P...) -> R) cached <P, R> (function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)
|