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.
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 }
=> falseVerify 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}
=> trueLoops over collection, executing block for each element.
let array = [1,2,3]
collection.ck_each { element in
print(element)
}
=> 1 2 3Find first object, that match provided block
let collection = [1,2,3]
println(collection.ck_match {(element) -> Bool in return element > 2})
=> 3Verify that all objects in collection do not match the block.
let collection = [1,2,3]
collection.ck_none { (element) -> Bool in return element < 0 }
=> trueFilter 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]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]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"]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"]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"]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"]- iOS 8 and higher / Mac OS X 10.10 or higher
- Swift 2
- CocoaPods
pod 'ClosureKit', '~> 0.3.0'- Carthage
github "DenHeadless/ClosureKit"



