ExSwift

by pNre

pNre / ExSwift

A set of Swift extensions for standard types and classes.

3.4K Stars 339 Forks Last release: over 5 years ago (v0.2.0) Other 311 Commits 11 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

ExSwift

CocoaPods Carthage compatible Build Status

Set of Swift extensions for standard types and classes.

Installation

Because of Xcode errors it's not possible to integrate this project with Cocoapods or as Embedded Framework. Read more at Dev Forum

Use submodule and copy source code

  1. Add ExSwift as a submodule
  2. Open the
    ExSwift
    project folder, and drag
    ExSwift
    sub folder with source code into the file navigator of your Xcode project. Make sure you select add to target
  3. Use it
components.takeFirst() { $0.completed }

Contents

Extensions

Array

Examples in the Wiki

Instance Methods

Name

Signature
first

first () -> Element?
last
last () -> Element?
get
get (index: Int) -> Element?
remove
remove  (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  (item: T...) -> Bool
difference
difference  (values: [T]...) -> [T]
intersection
intersection  (values: [U]...) -> Array
union
union  (values: [U]...) -> Array
unique
unique  () -> [T]
indexOf
indexOf  (item: T) -> Int?
indexOf
indexOf (condition: Element -> Bool) -> Int?
lastIndexOf
lastIndexOf  (item: T) -> Int?
zip
zip (arrays: Array...) -> [[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  (cond: (Element) -> T) -> [Array]
shuffle
shuffle ()
shuffled
shuffled () -> Array
sample
(random)
sample (size n: Int = 1) -> [T]
max
max  () -> T
min
min  () -> T
each
each (call: (Element) -> ())

each (call: (Int, Element) -> ())
eachRight
eachRight (call: (Element) -> ())

eachRight (call: (Int, Element) -> ())
any
any (call: (Element) -> Bool) -> Bool
all
all (call: (Element) -> Bool) -> Bool
reject
reject (exclude: (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  (groupingFunction group: (Element) -> (U)) -> [U: Array]
countBy
countBy  (groupingFunction group: (Element) -> (U)) -> [U: Int]
countWhere
countWhere (test: (Element) -> Bool) -> Int
reduce
reduce (combine: (Element, Element) -> Element) -> Element?
reduceRight
reduceRight (initial: U, combine: (U, Element) -> U) -> U
mapFilter
mapFilter  (mapFunction map: (Element) -> (V)?) -> [V]
implode
implode  (separator: C) -> C?
flatten
flatten  () -> [OutType]
flattenAny
flattenAny () -> [AnyObject]
toDictionary
toDictionary  (keySelector:(Element) -> U) -> [U: Element]
toDictionary
toDictionary  (transform: (Element) -> (key: K, value: V)?) -> [K: V]
cycle
cycle (n: Int? = nil, block: (T) -> ())
bSearch
bSearch (block: (T) -> (Bool)) -> T?
bSearch
bSearch (block: (T) -> (Int)) -> T?
sortUsing
sortUsing(block: ((T) -> U)) -> [T]
transposition
transposition (array: [[T]]) -> [[T]]
permutation
permutation (length: Int) -> [[T]]
repeatedPermutation
repeatedPermutation(length: Int) -> [[T]]
combination
combination (length: Int) -> [[Element]]
repeatedCombination
repeatedCombination (length: Int) -> [[Element]]

Class Methods

Name

Signatures
range

range  (range: Range) -> Array

Operators

Name

Signature Function

-
|
-  (first: Array, second: Array) -> Array
|Difference
-
|
-  (first: Array, second: T) -> Array
|Element removal
&
|
&  (first: Array, second: Array) -> Array
|Intersection ||| (first: Array, second: Array) -> Array|Union
* Int
|
*  (array: ItemType[], n: Int) -> [ItemType]
|Returns a new array built by concatenating int copies of self
* String
|
* (array: String[], separator: String) -> String
|Equivalent to
array.implode(String)
[rangeAsArray: x..y]

[rangeAsArray: x...y]
|
subscript(#rangeAsArray: Range) -> Array
|Returns the sub-array from index x to index y
[x, y, ...]
|
subscript(first: Int, second: Int, rest: Int...) -> Array
|Returns the items at x, y

Int

Examples in the Wiki

Properties

Name

NSTimeInterval
years
|
NSTimeInterval
year
|
NSTimeInterval
days
|
NSTimeInterval
day
|
NSTimeInterval
hours
|
NSTimeInterval
hour
|
NSTimeInterval
minutes
|
NSTimeInterval
minute
|
NSTimeInterval
seconds
|
NSTimeInterval
second
|

Instance Methods

Name

Signatures
times

times  (call: (Int) -> T)

times  (call: () -> T)

times (call: () -> ())
isEven
isEven () -> Bool
isOdd
idOdd () -> Bool
upTo
upTo (limit: Int, call: (Int) -> ())
downTo
downTo (limit: Int, call: (Int) -> ())
clamp
clamp (range: Range) -> Int

clamp (min: Int, max: Int) -> Int
isIn
isIn (range: Range, strict: Bool = false) -> Bool
digits
digits () -> Array
abs
abs () -> Int
gcd
gcd (n: Int) -> Int
lcm
lcm (n: Int) -> Int

Class Methods

Name

Signatures
random

random(min: Int = 0, max: Int) -> Int

Float

Examples in the Wiki

Instance Methods

Name

Signature
abs

abs () -> Float
sqrt
sqrt () -> Float
round
round () -> Float
ceil
ceil () -> Float
floor
floor () -> Float
clamp
clamp (min: Float, _ max: Float) -> Float

Class Methods

Name

Signatures
random

random(min: Float = 0, max: Float) -> Float

String

Examples in the Wiki

Properties

Name

length
capitalized

Instance Methods

Name

Signature
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
ltrimmed
ltrimmed (set: NSCharacterSet) -> String
rtrimmed
rtrimmed () -> String
rtrimmed
rtrimmed (set: NSCharacterSet) -> String
trimmed
trimmed () -> String
rtrimmed
rtrimmed (set: NSCharacterSet) -> String
toDouble
toDouble() -> Double?
toFloat
toFloat() -> Float?
toUInt
toUInt() -> UInt?
toBool
toBool() -> Bool?
toDate
toDate(format : String? = "yyyy-MM-dd") -> NSDate?
toDateTime
toDateTime(format : String? = "yyyy-MM-dd hh-mm-ss") -> NSDate?

Class Methods

Name

Signature
random

func random (var length len: Int = 0, charset: String = "...") -> String

Operators

Name

Signature

[x]
|
subscript(index: Int) -> String?
[x..y]

[x...y]
|
subscript(range: Range) -> String
[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

Range

Examples in the Wiki

Instance Methods

Name

Signatures
times

times (call: (T) -> ())

times (call: () -> ())
each
each (call: (T) -> ())
toArray
toArray () -> [T]

Class Methods

Name

Signature
random

random (from: Int, to: Int) -> Range

Operators

Name

Signature Function

=
|
==  (first: Range, second: Range) -> Bool
|Compares 2 ranges

Dictionary

Examples in the Wiki

Instance Methods

Name

Signatures
difference

difference  (dictionaries: [Key: V]...) -> [Key: V]
union
union (dictionaries: [Key: Value]...) -> [Key: Value]
intersection
intersection  (dictionaries: [K: V]...) -> [K: V]
has
has (key: Key) -> Bool
map
map  (mapFunction map: (Key, Value) -> (K, V)) -> [K: V]
mapFilter
mapFilter  (mapFunction map: (Key, Value) -> (K, V)?) -> [K: V]
mapValues
mapValues  (mapFunction map: (Key, Value) -> (V)) -> [Key: V]
mapFilterValues
mapFilterValues  (mapFunction map: (Key, Value) -> V?) -> [Key: V]
each
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  (groupingFunction group: (Key, Value) -> (T)) -> [T: Array]
countBy
countBy  (groupingFunction group: (Key, Value) -> (T)) -> [T: Int]
countWhere
countWhere (test: (Key, Value) -> (Bool)) -> Int
any
any (test: (Key, Value) -> (Bool)) -> Bool
all
all (test: (Key, Value) -> (Bool)) -> Bool
reduce
reduce  (initial: U, combine: (U, Element) -> U) -> U
pick
,
at
pick (keys: [Key]) -> Dictionary

pick (keys: Key...) -> Dictionary

at (keys: Key...) -> Dictionary
toArray
toArray  (mapFunction map: (Key, Value) -> V) -> [V]

Operators

Name

Signature Function

-
|
-  (first: Dictionary, second: Dictionary) -> Dictionary
|Difference
&
|
&  (first: Dictionary, second: Dictionary) -> Dictionary
|Intersection ||| (first: Dictionary, second: Dictionary) -> Dictionary|Union

NSArray

Examples in the Wiki

Instance Methods

Name

Signatures
cast

cast  () -> [OutType]
flatten
flatten  () -> [OutType]
flattenAny
flattenAny () -> [AnyObject]

SequenceOf

The following operations can be performed on sequences and are evaluated lazily. Each operation only takes the data it requires from the source sequence in order to return its result.

The

Sequence
protocol cannot be extended, hence the following are extensions to
SequenceOf
. They can be used as follows:
var source: Sequence = ...
var filteredSequence = SequenceOf(source).filter { ... }

Instance Methods

Name

Signatures
first

first () -> T?
any
any (call: (T) -> Bool) -> Bool
get
get (index: Int) -> T?
get
get (range: Range) -> SequenceOf
indexOf
indexOf  (item: U) -> Int?
filter
filter(include: (T) -> Bool) -> SequenceOf
reject
reject (exclude: (T -> Bool)) -> SequenceOf
skipWhile
skipWhile(condition:(T) -> Bool) -> SequenceOf
skip
skip (n:Int) -> SequenceOf
contains
contains (item: T) -> Bool
take
take (n:Int) -> SequenceOf
takeWhile
takeWhile (condition:(T?) -> Bool) -> SequenceOf

Double

Examples in the Wiki

Instance Methods

Name

Signature
abs

abs () -> Double
sqrt
sqrt () -> Double
round
round () -> Double
ceil
ceil () -> Double
floor
floor () -> Double
clamp
clamp (min: Double, _ max: Double) -> Double
roundToNearest
roundToNearest(increment: Double) -> Double

Class Methods

Name

Signatures
random

random(min: Double = 0, max: Double) -> Double

NSDate

Properties

Name

Signatures
year

Int
month
Int
weekday
Int
weekMonth
Int
days
Int
hours
Int
minutes
Int
seconds
Int

Instance Methods

Name

Signatures
add

add(seconds:Int=0, minutes:Int = 0, hours:Int = 0, days:Int = 0, weeks:Int = 0, months:Int = 0, years:Int = 0) -> NSDate
addSeconds
addSeconds (seconds:Int) -> NSDate
addMinutes
addMinutes (minute:Int) -> NSDate
addHours
addHours(hours:Int) -> NSDate
addDays
addDays(days:Int) -> NSDate
addWeeks
addWeeks(weeks:Int) -> NSDate
addMonths
addMonths(months:Int) -> NSDate
addYears
addYears(years:Int) -> NSDate
isAfter
isAfter(date: NSDate) -> Bool
isBefore
isBefore(date: NSDate) -> Bool
getComponent
getComponent (component : NSCalendarUnit) -> Int

Operators

Name

Signatures
==

==(lhs: NSDate, rhs: NSDate) -> Bool
<
 Bool
>
>(lhs: NSDate, rhs: NSDate) -> Bool
<=
<=(lhs: NSDate, rhs: NSDate) -> Bool
>=
>=(lhs: NSDate, rhs: NSDate) -> Bool
==
==(lhs: NSDate, rhs: NSDate) -> Bool

Utilities

Examples in the Wiki

Class Methods

Name

Signatures
after

after 

(n: Int, function: P -> T) -> (P -> T?)


func after  (n: Int, function: () -> T) -> (() -> T?)
once
once 

(function: P -> T) -> (P -> T?)


once  (call: Void -> T) -> (Void -> T?)
partial
partial 

(function: (P...) -> T, _ parameters: P...) -> ((P...) -> T?)

bind
bind 

(function: (P...) -> T, _ parameters: P...) -> (() -> T)

cached
cached 

(function: P -> R) -> (P -> R)


cached 

(function: (P...) -> R) -> ((P...) -> R)


cached 

(function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)

Operators

Name

Signatures
<=>

<=> (lhs: T, rhs: T) -> Int

To Do

  • [X] Wiki
  • [X] Xcode project for both iOS & OS X
  • [X] Review code comments
  • [ ] Example project
  • [ ] Installation instructions
  • [ ] Benchmark

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.