{ } Kit
BlocksKit is great. It allows to replace boring delegate callbacks, target-actions with blocks, making code more readable, and allowing to stay in context of what you are implementing. But with introduction of Swift language there are parts of BlocksKit that fall flat. First of all, lot of methods use id
, which is bridged to AnyObject!
in Swift, which does require explicit casts and type checks before you can write any code for objects in closures. Second - it does not allow you to use Swift structs and enums.
ClosureKit
bridges this gap. It provides generic implementation for the same methods as BlocksKit, that allows you to skip type checks and casts, and also allows usage of pure Swift enums and structs.
This project is NOT a replacement for BlocksKit. It's goal is to provide a more convenient API to Swift developers, that BlocksKit gives for Objective-C. ClosureKit also does not have any external dependencies except Swift standard library.
Contents
Features
CollectionType
ck_all
All - Verify, that all objects in collection match provided block.
let collection = [1,2,3]
collection.ck_all { (element) -> Bool in return element > 0 }
=> true
collection.ck_all { (element) -> Bool in return element < 2 }
=> false
ck_any
Any - Verify that at least one object in collection matches the block.
let collection = [1,2,3]
collection.ck_any {(element) -> Bool in return element < 0}
=> false
collection.ck_any { (element) -> Bool in return element > 2}
=> true
ck_match
Match - Find first object, that match provided block
let collection = [1,2,3]
println(collection.ck_match {(element) -> Bool in return element > 2})
=> 3
ck_none
None - Verify that all objects in collection do not match the block.
let collection = [1,2,3]
collection.ck_none { (element) -> Bool in return element < 0 }
=> true
Array
ck_performSelect
Perform select - Filter array, deleting all objects, that do not match block
var array = [1, 2, -1, -2, 3]
array.ck_performSelect { (element) -> Bool in return element > 0 }
println(array)
=> [1,2,3]
ck_performReject
Perform reject - Filter array, deleting all objects, that match block. This is reverse method to ck_performSelect.
var array = [1,2,-1,-2,3]
array.ck_performReject { (element) -> Bool in return element < 0 }
println(array)
=> [1,2,3]
Dictionary
ck_performSelect
Perform select - Filter dictionary, deleting all key-value pairs, that do not match provided block.
var dictionary = [1:"a",2:"b",3:"c"]
dictionary.ck_performSelect { (key,value) -> Bool in return key > 1}
=> [2:"b",3:"c"]
ck_performReject
Perform reject - Filter dictionary, deleting all key-value pairs, that match provided block. This is reverse for ck_performSelect
method.
var dictionary = [1:"a",2:"b",3:"c"]
dictionary.ck_performReject { (key,value) -> Bool in return key < 2}
=> [2:"b",3:"c"]
ck_reject
Reject - Find key-value pairs, that do not match provided block. This is reverse of ck_select method.
let dictionary = [1:"a",2:"b",3:"c"]
dictionary.ck_reject { (key,value) -> Bool in return key < 2}
=> [2:"b",3:"c"]
ck_select
Select - Find all key-value pairs, that match provided block.
let dictionary = [1:"a",2:"b",3:"c"]
dictionary.ck_select { (key,value) -> Bool in return key > 1}
=> [2:"b",3:"c"]
Requirements
- iOS 8 / macOS 10.10 / tvOS 9.0 / watchOS 2.0
- Swift 3
- Xcode 8
Installation
- CocoaPods
pod 'ClosureKit', '~> 1.0.0'
- Carthage
github "DenHeadless/ClosureKit"